[英]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:是否可以过滤 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 tried this solution but it is not working我尝试了这个解决方案,但它不起作用
this.centers = this.centers.filter(element => {
return element.isiedaCcaa.id == 1;
})
You have a typo on the return.您在退货时有错字。
should be return element.iseidaCcaa.id == 1
应该
return element.iseidaCcaa.id == 1
Although I would consider using === if the id is going to be int虽然我会考虑使用 === 如果 id 将是 int
You wrong property by type iseidaCcaa not isiedaCcaa您按类型 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.