0

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

7
  • property isKnowledgeSkills does not exist on x.subCategories, because x.subCategories is 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 have isKnowledgeSkills === true, or some other condition Commented May 20, 2021 at 7:44
  • 1
    Adding desired output to the question would help to understand it. Commented May 20, 2021 at 8:21
  • In this above array object I need to fetch the data which is having isKnowledgeSkills true Commented May 20, 2021 at 8:25
  • I need to apply the filter option for both parent and child objects Commented May 20, 2021 at 8:26
  • Have added the example result as well Commented May 20, 2021 at 8:29

3 Answers 3

3

I would go with the following:

const filtered = knowledgeData.filter(p => p.isParentKnowledgeSkills)
    .map(p => ({
        ...p,
        subCategories: p.subCategories.filter(s => s.isKnowledgeSkills)
    }));
  • first filter out parent data with isParentKnowledgeSkills
  • secondly, filter out child data

Playground link

Sign up to request clarification or add additional context in comments.

Comments

1

change your if condition

if(x.isParentKnowledgeSkills === true 
      && ( x.subCategories.filter((y) => y.isKnowledgeSkills ===true).length )){
            return true
          }

1 Comment

I need to fetch the data which is having isKnowledgeSkills true
0

I have found the answer...

this.knowledgeData = this.knowledgeData.map((i)=>{
          i.subCategories = i.subCategories.filter((x)=> x.isKnowledgeSkills === true)
          return i;
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.