I have array object i need to use some condition for fetch the data.
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
},
]
}
]
Have used filter option to find the datas with satisfied conditions..
this.knowledgeData = this.knowledgeData.filter((x)=>{
if(x.isParentKnowledgeSkills ===true && x?.subCategories?.isKnowledgeSkills ===true){
return true
}
})
Its return empty... I need to find the data only the parent and child value return true
result should be like following
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
},
]
}
]
which means output should return the object which is having isKnowledgeSkills is true in child array subCategories
isKnowledgeSkillsdoes not exist onx.subCategories, becausex.subCategoriesis an Array. It is property of elements of this Array, not the Array itself. Having said that, it is unclear what filter condition you are after - if there is a subcategory with isKnowledgeSkills === true, or if all subcategories must haveisKnowledgeSkills === true, or some other condition