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

111
Visualizações
How to traverse through a tree like nested data structure of unknown depth in order to find and collect addressable array items?

Say I have an array that looks as such:

[{
  "name": "Audiograms",
  "folders": [{
    "name": "2022"
  }, {
    "name": "2021"
  }, {
    "name": "2020"
  }]
}, {
  "name": "Patient Paperwork"
}, {
  "name": "Repairs"
}]

And this array can have an infinite amount of objects and sub-objects, similar to a file tree.

I have an array letting me know the name of the folders I need to access from the root of the object, like:

["Audiograms", "2022"]

I also do not know this value ahead of time, nor do I know how many items are in this array ahead of time.

How would I be able to actually traverse this file tree using the array of names? I wish to do things like maybe pop the matching object out and move it to another part of the file tree.

Thank you!

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

0

OP

"I wish to do things like maybe pop the matching object out and move it to another part of the file tree."

In order to achieve follow-up tasks like the above mentioned one, the next provided solution walks the OP's folder structure and collects for each addressable match an object of two references, target and parent, where the former is the reference of the to be found folder-item, and the latter is the reference of its parent folder-item.

The solution got achieved by a recursively implemented reducer function.

function collectAddressableFolderRecursively(collector, folderItem) {
  const { name = null, folders = [] } = folderItem;
  const {
    address: [parentName, childName], result,
  } = collector;

  if (name === parentName && folders.length) {
    const targetFolder = folders
      .find(({ name }) => name === childName) ?? null;

    if (targetFolder !== null) {
      result.push({
        target: targetFolder,
        parent: folderItem,
      });
    }
  }
  result.push(
    ...folders.reduce(collectAddressableFolderRecursively, {
      address: [parentName, childName],
      result: [],
    })
    .result
  );
  return collector;
}

const folders = [{
  name: 'Audiograms',
  folders: [{
  
    name: '2022',
    folders: [{

      name: 'Audiograms',
      folders: [{
        name: '2022',
      }, {
        name: 'foo',
      }],
    }],
  }, {
    name: '2021',
  }, {
    name: '2020',
  }]
}, {
  name: 'Patient Paperwork',
}, {
  name: 'Repairs',
  folders: [{

    name: 'Audiograms',
    folders: [{
      name: '2022',
    }, {
      name: 'bar',
    }],
  }, {
    name: 'baz',
  }],
}]
const address = ['Audiograms', '2022'];

const { result } = folders
  .reduce(collectAddressableFolderRecursively, {
    address,
    result: [],
  });
console.log({ address, result });
.as-console-wrapper { min-height: 100%!important; top: 0; }

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