Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda