I am dealing with an array object and I need to apply certain conditions to 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
},
]
}
]
I utilized the filter option to locate the data that satisfies the conditions.
this.knowledgeData = this.knowledgeData.filter((x)=>{
if(x.isParentKnowledgeSkills ===true && x?.subCategories?.isKnowledgeSkills ===true){
return true
}
})
However, it is returning empty. I aim to retrieve data where both the parent and child values are true.
The desired result should be as follows:
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
},
]
}
]
This means that the output should only include objects where isKnowledgeSkills is true in the subCategories child array.