The specified 'contactId' property cannot be found within the data type of 'any[]'

I am attempting to filter an array of objects named 'notes'. However, when I attempt this, I encounter the following error: Property 'contactId' does not exist on type 'any[]'.

notes: Array < any > [] = [];
currentNotes: Array < any > [] = [];

notes.forEach(element => {
  //Filter out notes without contact  
  if (element.contactId != null) {
    this.currentNotes.push(element);
  }
})

Answer №1

If you want to create an array of arrays in your code, you can do it like this:

   data: Array < any > = [];
    newData: Array < any > = [];

    data.forEach(item => {
      //Filter out items without a certain property  
      if (item.property) {
        this.newData.push(item);
      }
    })

Answer №2

Before comparing, it's important to first check if the element contains a contactid or not.

notes: Array < any > [] = [];
    currentNotes: Array < any > [] = [];

    notes.forEach(element => {
      // Filter out notes without a contact  
      if (element.contactId&&element.contactId != null) {
        this.currentNotes.push(element);
      }
    })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to use openapi-generator with typescript-angular to generate just a module within an existing Angular project instead of creating a separate package?

Currently, I am utilizing the openapi-generator tool specifically for typescript-angular. Although I have been able to successfully generate an Angular module along with all its components, it results in a separate npm package. While I acknowledge the ad ...

Processing JSON using PHP and transforming it into an array

I've had limited experience working with JSON in the past, and now I can't remember how to use it with PHP. If there is a script that outputs JSON in this format: { "bunisess":[ "business", "bonuses", "burnooses", "boniness", ...

How do I insert a new column into the result set of a query in Prisma?

Imagine a scenario where there are two tables: User, which has fields for name and Id, and Post, which has fields for name and content. These tables are connected through a many-to-many relationship (meaning one post can have multiple users/authors and eac ...

Having trouble organizing the date strings in the material table column

In my Angular application, I have a material table with multiple columns that I am sorting using matSort. While I can successfully sort the last two columns in ascending or descending order, I am facing an issue with the first column which contains date va ...

Using Firestore to Query and Subscribe to Multiple Documents using an Array of IDs

Despite the limitations in querying multiple documents based on an Array of IDs, I frequently encounter situations where it becomes necessary. In this particular instance, I am faced with the challenge of selecting a random list of IDs which I believe is ...

Error: ionic serve is unable to locate the 'reflect-metadata' module

I am new to Ionic2 and following the steps: $ npm install -g ionic cordova $ ionic start cutePuppyPics --v2 $ cd cutePuppyPics $ ionic serve However, I encountered an error: "Cannot find module 'reflect-metadata' $ ionic info Cordova CLI: 6.5 ...

Angular Universal - Transfer state does not populate correctly when the URL contains unsafe characters, leading to duplicate XHR calls

Working with Angular(12) and Angular Universal has presented me with a challenge regarding the transfer state between the server and client side. On the client side, I am using the TransferHttpCacheModule, while on the server side module, I have implemente ...

Loop through the ng-content elements and enclose each one within its own individual nested div

Although it is currently not supported, I am curious to know if anyone has discovered a clever solution to this problem. In my current setup, I have a parent component with the following template: <dxi-item location='after' class="osii-item- ...

Access Denied: Origin not allowed

Server Error: Access to XMLHttpRequest at '' from origin 'http://localhost:4200' has been blocked by CORS policy. The 'Access-Control-Allow-Origin' header is missing on the requested resource. import { Injectable } from &apo ...

Tips on utilizing the i18n t function within a TypeScript-powered Vue3 component

I am working on creating a control that can automatically manage interpolation for internationalization (i18n) strings: <script lang="ts"> import { ref, defineComponent, inject } from "vue"; import type { InterpolationItem, Inter ...

What is the process for determining the element represented by *(arr+i)[1] and **(arr+i)?

I'm having trouble understanding how the elements below are being determined: When *(arr+1)[1], 7 is printed. And when **(arr+1), 4 is printed. #include <stdio.h> int main() { int arr[3][3]={1,2,3,4,5,6,7,8,9}; printf("%d %d",*(a ...

Executing functions in TypeScript

I am facing an issue while trying to call a function through the click event in my template. The error message I receive is "get is not a function". Can someone help me identify where the problem lies? This is my template: <button class="btn btn-prima ...

Struggling to implement a singleton service in Angular as per the documentation provided

I have implemented a service in Angular that I want to be a singleton. Following the guidelines provided in the official documentation, I have set the providedIn property to "root" as shown below: @Injectable({ providedIn: "root" }) export class SecuritySe ...

Issues with CodeIgniter Calendar Preferences template functionality

Check out the $prefs. $prefs = array( 'month_type' => 'long', 'day_type' => 'short', 'show_next_prev' => true, 'next_prev_url' => ...

Pass the identical event to multiple functions in Angular 2

On my homepage, there is a search form with an input box and select dropdown for users to search for other users by location or using html5 geolocation. When a user visits the page for the first time, they are prompted to allow the app to access their loca ...

Cannot establish a connection with Socket.IO

I've encountered an issue with establishing a connection to Socket.IO in my node server. Although the server starts successfully with Socket.IO, I am not seeing any console logs when trying to connect to Socket. this.server.listen(this.port, () => ...

What is the proper way to register ActivatedRoute, Route, and Http in Angular?

Where should Angular's libraries such as ActivatedRoute, Route, and Http be registered within the main NgModule? ActivatedRoute, Route, Http in Angular Since these are not services, pipes, or directives, they can be registered in either providers or ...

The dropdown feature in Bootstrap 5 seems to be malfunctioning in Angular 12

I am facing issues while trying to implement the Bootstrap 5 dropdown in Angular 12. After installing all required packages and adding them to the angular.json file, I still cannot get it to work properly. Even after copying the example directly from the ...

Two forms are present on one page, but the submit button is triggering validations for both forms simultaneously

Currently, I am facing an issue with two forms that share one submit button. The problem is that although I have implemented validation for both forms, the validations are not working separately. I want the validations to work independently - when I submit ...

Choosing between running a single unit test or multiple tests using Karma toggle switch

Currently, I am working on writing unit tests using the Karma/Jasmine combination and I'm curious if there's a way to target a specific spec file for testing instead of running all the spec files in the files array defined in the karma.conf.js fi ...