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

132
Visualizações
Get the index of the array when array object matching condition

I have an array like this:

parent = [
    {'id':'id_1','level':'1-1','children':['id':'id_1','level':'1-1-0','children':[........]},
    {'id':'id_2','level':'2-2','children':['id':'id_1','level':'2-1-0','children':[........]}
]

How can I get the parent array index that match the condition of child array object

For example:

If (level == '2-1-0') 

Output: 1

Expected Behaviour:

It should return 1 because level 2-1-0 present in 1'st index of parent array

Note: I tried like below

var index = parent .findIndex(data => data.level== result.level);

It will not check child array object

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It's a recursive problem, which can blow the stack for big arrays.

The script traverses the tree depth first and can be optimized to detect the requested level depth and only check that level, increasing the performance.

var weirdProblem = level => (carry, current, idx) => {
  // bail if already found 
  if (carry > -1) {
    return carry;
  }

  // check current item's children and return current idx if matching
  if (current.children.some(child => child.level === level)) {
    return idx;
  }

  // traverse current item's children
  return current.children.reduce(weirdProblem(level), -1);
};

var index = parent.reduce(weirdProblem('2-1-0'), -1);

did not test the script.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your provided array is wrong, but I think that you need something like this:

const parent = [
    {'id':'id_1','level':'1-1','children':[{'id':'id_1','level':'1-1-0'}]},
    {'id':'id_2','level':'2-2','children':[{'id':'id_1','level':'2-1-0'}]}
]

const index = parent.findIndex(p => p.children.find(c => c.level === '2-1-0') !== undefined);

console.log(index);

In the place of '2-1-0' you should put the index, that you want to check.

about 4 years ago · Juan Pablo Isaza Relatório

0

in .TS file:-

const parent = [
    {'id':'id_1',
     'level':'1-1',
     'children':[{'id':'id_1',
                  'level':'1-1-0'}]},
                 {'id':'id_2',
                  'level':'2-2',
                  'children':[{'id':'id_1',
                               'level':'2-1-0'}]}
]

const index = parent.filter(x => x.children.find(x => x.level === '2-1-0') !== undefined);

console.log(index);

Give this a try. I think it will work.

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