【发布时间】:2021-06-14 20:44:30
【问题描述】:
我正在尝试过滤一个类对象数组,其中又嵌套了类对象。我知道这很令人困惑,我将提供一个示例代码:
//class
export class Class1{
id:number;
team: Teams // here another class contains 'id' and 'name'
}
// Assume that the variable this.tmpObj contains array of Class1 objects
tmpObj:Class1[];
所以在这里我只想获取没有 team.id 100 的 Class1 对象。
this.tmpObj= this.tmpObj.filter(({ team }) => {
return team.id != 100
})
但此代码显示 team.id 为空的错误。我尝试了其他一些过滤方式。但同样的错误。 任何想法是什么错误。提前致谢。
【问题讨论】:
-
this.tmpObj= this.tmpObj.filter((team) => team.id !== 100) ?
-
你应该
console.log你的tmpObj看看它是否包含你期望它包含的内容。 -
@MichaelDesigaud 未过滤 team.id =100 的对象。
-
正如我之前所说,我怀疑您的对象看起来不像您期望的那样。请在循环播放之前显示您的
this.tmpObj的console.log。 -
@Random 由于上一行出现错误,它不打印
标签: angular typescript filter