Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
filter nested array of objects on children conditions

I have an array of objects with category trees containing children arrays. Each category has a property disabled which may be true or false. I need to collect an array of all parents ids that has to be set disabled true if all of its bottom most children has disabled true.

 [
  {
    Category: {
      id: "69",
      createdAt: "2022-05-24T09: 54: 27.104Z",
      updatedAt: "2022-05-25T10: 36: 14.168Z",
      name: "Jewelry",
      key: "prykrasy",
      description: "Прикраси",
      disabled: false,
      mpath: "69.",
      children: [
        {
          Category: {
            id: "70",
            createdAt: "2022-05-24T09: 54: 27.109Z",
            updatedAt: "2022-05-25T10: 36: 14.156Z",
            name: "Accessories",
            key: "aksesyary-dlya-prykras",
            description: "Аксесуари для прикрас",
            disabled: false,
            mpath: "69.70.",
            children: [
              
            ],
            
          },
          Category: {
            id: "71",
            createdAt: "2022-05-24T09: 54: 27.115Z",
            updatedAt: "2022-05-25T10: 36: 14.156Z",
            name: "Silver",
            key: "bizhuteriya",
            description: "Silver",
            disabled: false,
            mpath: "69.71.",
            children: [
              
            ],
            
          },
          Category: {
            id: "72",
            createdAt: "2022-05-24T09: 54: 27.121Z",
            updatedAt: "2022-05-25T10: 36: 14.168Z",
            name: "jlewelry-stuff",
            key: "uvelirni-vyroby",
            description: "Ювелірні вироби",
            disabled: true,
            mpath: "69.72.",
            children: [
              
            ]
          }
        }
      ]
    }
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I created a function to check the Category object for two things:

  1. Do all the children have disabled set as true?
  2. Does each child have children?

For case 1, it stores the id in a variable you can access. For case 2, it runs the same check for the children Category objects.

const allDisabled = []; // stores the ids

const checkChildren = (category) => {    
    let disabledCount = 0;
    for (let i = 0; i < category.children.length; i++) {
        const ctg = category.children[i].Category; // child category

        if (ctg.disabled) {
            disabledCount++;
        }

        if (ctg.children.length) {
            checkChildren(ctg);
        }
    }
    if (disabledCount === category.children.length) {
        // all children are disabled
        allDisabled.push(category.id);
    }
}

Now you can run this function.

const categoriesArray = [...];

categoriesArray.forEach(item => checkChildren(item.Category));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!