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

113
Views
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 answers
Answer question

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 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!