【发布时间】:2021-08-09 10:38:33
【问题描述】:
我有数组对象,我需要使用一些条件来获取数据。
this.knowledgeData = [
{
"id": 3,
"name": "Education",
"isOtherCategory": 0,
"isKnowledgeSkills": false,
"isMyInterest": false,
"isParentKnowledgeSkills": true,
"isParentMyInterest": false,
"subCategories": [
{
"id": 96,
"categoryId": 3,
"name": "Careers",
"isOtherSubCategory": 0,
"isKnowledgeSkills": false,
"isMyInterest": false
},
{
"id": 97,
"categoryId": 3,
"name": "General",
"isOtherSubCategory": 0,
"isKnowledgeSkills": true,
"isMyInterest": false
},
{
"id": 92,
"categoryId": 3,
"name": "Home Schooling",
"isOtherSubCategory": 0,
"isKnowledgeSkills": true,
"isMyInterest": false
},
]
}
]
已使用过滤器选项找到满足条件的数据..
this.knowledgeData = this.knowledgeData.filter((x)=>{
if(x.isParentKnowledgeSkills ===true && x?.subCategories?.isKnowledgeSkills ===true){
return true
}
})
它的返回为空...我需要找到只有父子值返回true的数据
结果应该如下所示
this.knowledgeData = [
{
"id": 3,
"name": "Education",
"isOtherCategory": 0,
"isKnowledgeSkills": false,
"isMyInterest": false,
"isParentKnowledgeSkills": true,
"isParentMyInterest": false,
"subCategories": [
{
"id": 97,
"categoryId": 3,
"name": "General",
"isOtherSubCategory": 0,
"isKnowledgeSkills": true,
"isMyInterest": false
},
{
"id": 92,
"categoryId": 3,
"name": "Home Schooling",
"isOtherSubCategory": 0,
"isKnowledgeSkills": true,
"isMyInterest": false
},
]
}
]
这意味着输出应该返回子数组 subCategories 中 isKnowledgeSkills 为 true 的对象
【问题讨论】:
-
属性
isKnowledgeSkills在x.subCategories上不存在,因为x.subCategories是一个数组。它是该数组元素的属性,而不是数组本身。话虽如此,目前尚不清楚您所追求的过滤条件 - 如果存在 isKnowledgeSkills === true 的子类别,或者是否所有子类别都必须具有isKnowledgeSkills === true或其他一些条件 -
在问题中添加所需的输出将有助于理解它。
-
在上面这个数组对象中,我需要获取 isKnowledgeSkills 为 true 的数据
-
我需要为父对象和子对象应用过滤器选项
-
也添加了示例结果
标签: angular typescript