How to Use Angular 8 to Filter an Array of Objects Based on Numeric Values

Can the list be filtered based on the value of iseidaCcaa.id?

var centers = [
  {
    id: 2,
    nombre: "Centro 2",
    iseidaCcaa: {
      id: 1,
    },
  },
  {
    id: 3,
    nombre: "Centro 3",
    iseidaCcaa: {
      id: 1,
    },
  },
  {
    id: 1,
    nombre: "Centro 1",
    iseidaCcaa: {
      id: 2,
    },
  },
];

I attempted this solution, but unfortunately it's not working as expected:

 this.centers = this.centers.filter(element => {
    return element.isiedaCcaa.id == 1;
  })

Answer №1

There is a mistake in the return statement.

The correct syntax should be

return element.iseidaCcaa.id == 1

If the id is going to be an integer, it's better to use === instead of ==.

Answer №2

The property you are looking for is iseidaCcaa, not isiedaCcaa

var centers = [    {
       "id":2,
       "nombre":"Centro 2",
       "iseidaCcaa":{
          "id":1,
       }

    },
    {
       "id":3,
   "nombre":"Centro 3",
   "iseidaCcaa":{
     "id":1,
   }
},
{
   "id":1,
   "nombre":"Centro 1",
   "iseidaCcaa":{
     "id":2,
   }
}
]

 var centers = centers.filter(element => {
    return element.iseidaCcaa.id == 1;
  })
  
  console.log(centers);

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

Transmit a base64-encoded image in an HTTP request to the server

I am currently working on sending a base64 image using the request npm module from a nodejs/express application to another REST API endpoint. Below is the code I am using: First, I have middleware set up using multer and datauri to upload the image in mem ...

Retrieve the personalized data-* attributes from a specific item within an array

I'm struggling to find the correct syntax for accessing custom data-* attributes for list elements within an unordered list that has been sorted and converted into an array using the following code: $("#gridlist").sortable("toArray"); The list eleme ...

Navigating through various Headers in ReactjsHandling different Headers in Reactjs

I am currently working on a project using Reactjs (Nextjs) and I need to have different headers for the home page compared to the rest of the pages. I have created a "Layout.js" file where I have placed the header and footer components, and then imported ...

What are the steps for integrating Angular Material into an existing project?

I have recently set up an Angular 2 project. Attempting to integrate Angular Material into my existing project, I followed the steps outlined in the official documentation by running the npm command: npm install --save @angular/material @angular/cdk How ...

Here is the process to make a GET request to an API with input parameters using JQuery and JavaScript in an Asp.net view when a particular tag

Seeking assistance in displaying the count of orders on my view using a special tag. I have written a stored procedure in SQL that returns the order count based on the input type. To showcase all types of orders, I have designed multiple boxes on my form. ...

Exploring the animation potential of HTML5 canvas and Javascript through utilizing putImageData with animated gifs

I am interested in modifying the image data of each frame in an animated gif while it is playing in a web browser, using HTML5 canvas and Javascript. For instance, I would like to convert every frame to grayscale dynamically as the gif plays. Is this achie ...

Using jQuery to select all child elements based on a specified condition

Is there a way to locate all instances of .post-comment-replies that have a nested '.post-comment-reply' within them, rather than being at the first level? Currently, the code provided retrieves not only those .post-comment-replies with nested . ...

Generating dynamic menu options for a <select> element from an array using React's createElement() method

I have a question about dynamically populating options from an array or JSON. Currently, I am generating the options manually with the following code: return React.createElement("select", {}, React.createElement("option", {value: "A"}, "Option ...

What is the reason behind Bootstrap 5's decision to have the getOrCreateInstance() method automatically toggle the collapsible element upon creation?

Today was my first encounter with the Collapse feature in Bootstrap 5, and I have to admit, I am not a fan! Imagine a scenario where I have a collapsible element that is initially hidden, and a button to toggle its visibility, like so: <button class=&q ...

Learn how to securely transmit data using basic authentication in Node.js with the node-fetch module

Having trouble with this code. I am facing an issue where a "post" request with basic authentication returns a 200 status, but no response data is received. Surprisingly, when the same code is executed without auth credentials, it works perfectly fine. l ...

How to retrieve the type of a computed keyof T as a generic type within TypeScript

I am working with two different interfaces: interface PersonRequirements{ user:string, password:string, id:number } export interface Requirement<R> { name: keyof R & string, save: () => any,/* I want this return type to be ...

Trigger the component method in vue.js upon loading

Hello, I am still learning Vue.js and I am facing a challenge that seems simple but I am struggling to solve it. My goal is to make one of the component's methods execute every time it is loaded into the DOM. I have tried using v-on:load but it doesn& ...

Encountering a problem while trying to incorporate Mapbox GL JS into an Angular 8 web application

I'm currently working on incorporating mapbox into my simple web application, but I'm encountering difficulties when attempting to add it. At this point, I've already created a mapbox service and a map component. My approach involved using ...

The URL is reverted back to the previous address

Currently in the process of developing an Angular application, I've encountered a minor visual issue. On one of the pages, there is a ReactiveForm implemented, but whenever I navigate to that page, the URL reverts back to the previous one (even though ...

Guide on creating a detailed list of categories mapped to specific classes that all adhere to a common generic standard

Most TypeScript factory patterns I've encountered rely on a named mapping between a name and the Class type. A basic implementation example: const myMap = { classOne: ExampleClass, classTwo: AnotherClass } (k: string) => { return new myMap[k] } ...

JSX conditionally rendering with an inline question: <option disabled value="">Select an option</option>

Yes, I can confirm that the inline is functioning properly because in the Convert HK to Passive Segment paragraph at the top I am seeing the expected output. What I am aiming for is to display a "Choose a hotel" message when there are multiple hotels in th ...

When attempting to execute a query in Node using oracledb, an error with the code 'npm ERR! errno 3221225477' occurred

Encountered the following error message in the command line: npm ERR! code ELIFECYCLE npm ERR! errno 3221225477 npm ERR! [email protected] start: `node ./bin/www` npm ERR! Exit status 3221225477 npm ERR! npm ERR! Failed at the [email protected] start sc ...

Simple Solutions for Moving Containers Up in Angular When Clicked

In the left side container, I have a header and sub-header, while the right side container contains text. However, whenever I click on the sub-header to display text on the right side, both container positions shift upward, hiding the header text. I'm ...

How can I correctly annotate property 'firebase' on type '{ children?: ReactNode; }' in order to resolve the error?

My firebase object is provided through a higher order component as shown below: const FirebaseContextInterfaceLocal: FirebaseContextInterface = { value: new Firebase() } ReactDOM.render( <Router history={history}> <FirebaseContext.Provi ...

Executing concurrent processes in React components using Redux

Is there an optimal way to execute a time-consuming network request as a background task within a React component? For instance, I need to load a component that is available to the user while simultaneously making a demanding network request in the backgr ...