Oussama EL
Oussama EL

Reputation: 154

angular 8 - Filter an Array of objects by number

Is it possible to filter this list where iseidaCcaa.id is equal to a given number:

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 tried this solution but it is not working

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

Upvotes: 2

Views: 15922

Answers (2)

Hien Nguyen
Hien Nguyen

Reputation: 18975

You wrong property by type 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);

Upvotes: 1

Alboman
Alboman

Reputation: 325

You have a typo on the return.

should be return element.iseidaCcaa.id == 1

Although I would consider using === if the id is going to be int

Upvotes: 2

Related Questions