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

92
Views
JavaScript nth degree tree
  1. I need to change the clicked property to true of a parent if all children clicked are true. so in this case ID - 14,15 is having clicked property to true. So, parents having ID 11 clicked has to be made true.
  2. If 11,12,14,15 are true then then 4 has to be made true.
let obj = {children:[
{
    ID:1,
    clicked: false,
    children: [
        {
            ID:4,
            clicked: false,
            children: [
                {
                    ID:11,
                    clicked: false,
                    children: [
                                {
                                    ID:14,
                                    clicked: true,
                                },
                                {
                                    ID:15,
                                    clicked: true,
                                }
                            ]
                },
                {
                    ID:12,
                    clicked: false,
                }
            ]
        },
        {
            ID:5,
            clicked: false,
        }
    ]
  }
 ]
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could take a depth-first search algorithm and if all of the children contains clicked: true, the parent gets an update.

const
    update = object => {
        if (!object.children) return object.clicked;
        return object.clicked = object.children.every(update);
    },
    object = { children: [{ ID: 1, clicked: false, children: [{ ID: 4, clicked: false, children: [{ ID: 11, clicked: false, children: [{ ID: 14, clicked: true }, { ID: 15, clicked: true }] }, { ID: 12, clicked: false }] }, { ID: 5, clicked: false }] }] };

update(object);

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Nina gave a great answer for when you want to mutate the input.

Here is a simple one which creates a new object:

const consolidate = ({clicked, children = [], ...rest}, _, __, kids = children .map (consolidate)) => ({
  ...rest, 
  clicked: clicked || (kids .length > 0) && kids .every (k => k .clicked),
  ...(kids .length ? {children: kids} : {})
})

const obj1 = {children: [{ID: 1, clicked: false, children: [{ID: 4, clicked: false, children: [{ID: 11, clicked: false, children: [{ID: 14, clicked: true}, {ID: 15, clicked: true}]}, {ID: 12, clicked: false}]}, {ID: 5, clicked: false}]}]};
//  with 12 switched to `true`
const obj2 = {children: [{ID: 1, clicked: false, children: [{ID: 4, clicked: false, children: [{ID: 11, clicked: false, children: [{ID: 14, clicked: true}, {ID: 15, clicked: true}]}, {ID: 12, clicked: true}]}, {ID: 5, clicked: false}]}]};

console .log ('obj1', consolidate (obj1))
console .log ('obj2', consolidate (obj2))
.as-console-wrapper {max-height: 100% !important; top: 0}

We recur on the children, if any, and then regenerate the object evaluating the clicked property of each.

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!