angular 8 - 按数字过滤对象数组
angular 8 - Filter an Array of objects by number
是否可以过滤此列表,其中 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,
},
},
];
我试过这个解决方案,但它不起作用
this.centers = this.centers.filter(element => {
return element.isiedaCcaa.id == 1;
})
您在 return 上有错字。
应该是return element.iseidaCcaa.id == 1
虽然我会考虑使用 === 如果 id 将是 int
你错了 属性 类型是 iseidaCcaa 而不是 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);
是否可以过滤此列表,其中 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,
},
},
];
我试过这个解决方案,但它不起作用
this.centers = this.centers.filter(element => {
return element.isiedaCcaa.id == 1;
})
您在 return 上有错字。
应该是return element.iseidaCcaa.id == 1
虽然我会考虑使用 === 如果 id 将是 int
你错了 属性 类型是 iseidaCcaa 而不是 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);