Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda