Reputation: 154
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
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
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