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

338
Views
Loop through a nested Object until a certain value is found, Trees

me and my partner have been cracking our heads at this. we have to create a tree, that obviously has "children" below it.

all we need right now, is to loop over an object to find a certain value, if that value is not in that certain object, then go into its child property and look there.

basically what I'm asking is, how can you loop over nested objects until a certain value is found?

would super appreciate the perspective of a more experienced coder on this.

/// this is how one parent with a child tree looks like right now, 
essentially if we presume that the child has another child in the children property, 
how would we loop into that? 
and maybe if that child also has a child, so on and so on...

Tree {
  value: 'Parent',
  children: [ Tree { value: 'Child', children: [] } ]
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use recursive function to loop through objects:

const Tree = {
  value: 'Parent',
  children: [
  {
    value: 'Child1',
    children: [
    ]
  },
  {
    value: 'Child2',
    children: [
    {
      value: 'Child2.1',
      children: [
      {
        value: 'Child2.1.1',
        children: [
        ]
      },
      {
        value: 'Child2.1.2',
        children: [
        ]
      },
      ]
    },
    ]
  },
  ]
}

function findValue(obj, value)
{
  if (obj.value == value)
    return obj;

  let ret = null;
  for(let i = 0; i < obj.children.length; i++)
  {
    ret = findValue(obj.children[i], value);
    if (ret)
      break;
  }
  return ret;
}

console.log("Child1", findValue(Tree, "Child1"));
console.log("Child2.1", findValue(Tree, "Child2.1"));
console.log("Child3", findValue(Tree, "Child3"));

about 4 years ago · Juan Pablo Isaza Report

0

You can try this simple solution:

const myTree = {
    value: 'Parent',
    children: [
        {
            value: 'Child1',
            children: []
        },
        {
            value: 'Child2'
        }
    ]
}

const isValueInTree = (tree, findValue) => {
    if (tree.value === findValue) return true;

    if (tree.children && tree.children.length !== 0) {
        for (const branch of tree.children) {
            const valuePresents = isValueInTree(branch, findValue);
            if (valuePresents) return true;
        }
    }
    
    return false;
}

console.log(isValueInTree(myTree, 'Child2'));
console.log(isValueInTree(myTree, 'Child'));
console.log(isValueInTree(myTree, 'Child1'));

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!