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

146
Views
Get the branch of collection without the sibling elements, searching by property

I have the object with the next structure:

let array = [
      {
        name: 'Name1',
        items: [
          {
            name: 'Name1.1',
            items: [
              { id: '1', name: 'Name1.1.1' },
              { id: '2', name: 'Name1.1.2' },
              { id: '3', name: 'Name1.1.3' },
              ...
            ],
          },
          {
            name: 'Name1.2',
            items: [
              { id: '4', name: 'Name1.2.1' },
              { id: '5', name: 'Name1.2.2' },
            ],
          },
        ],
      },
      {
        name: 'Name2',
        items: [
          {
            name: 'Name2.1',
            items: [
              { id: '6', name: 'Name2.1.1' },
              { id: '7', name: 'Name2.1.2' },
            ],
          },
        ],
      },
    ];

I want to get the branch without the sibling elements, searching by id. The desired result is the next structure by id = '4':

let array = [
      {
        name: 'Name1',
        items: [
          {
            name: 'Name1.2',
            items: [
              { id: '4', name: 'Name1.2.1' },
            ],
          },
        ],
      }
    ];

I could find only the end element of the tree ({ id: '4', name: 'Name1.2.1' }). But I don't understand how to get intermediate structures of the tree.

const test = (data, id) => {
    if (!data || !data.length) return null;

    for (var j = 0; j < data.length; j++) {
      var result = data[j].items
        ? test(data[j].items, id)
        : data[j].id
        ? data[j].id === id
          ? data[j]
          : undefined
        : undefined;

      if (result !== undefined) {
        return result;
      }
    }

    return undefined;
  };

test(array, '4');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should indeed take a recursive approach, but your function currently can only return an id value (a string) or null or undefined. It never returns an array, yet that is what you expect to get.

When a solution is found as a base case, you need to wrap that solution in an array and plain object, each time you get out of the recursion tree.

Here is a working solution:

function getPath(forest, targetid) {
    for (let root of forest) {
        if (root.id === targetid) return [root]; // base case
        let items = root.items && getPath(root.items, targetid);
        if (items) return [{ ...root, items }];  // wrap!
    }
}

// Example run:
let array = [{name: 'Name1',items: [{name: 'Name1.1',items: [{ id: '1', name: 'Name1.1.1' },{ id: '2', name: 'Name1.1.2' },{ id: '3', name: 'Name1.1.3' },],},{name: 'Name1.2',items: [{ id: '4', name: 'Name1.2.1' },{ id: '5', name: 'Name1.2.2' },],},],},{name: 'Name2',items: [{name: 'Name2.1',items: [{ id: '6', name: 'Name2.1.1' },{ id: '7', name: 'Name2.1.2' },],},],},];
console.log(getPath(array, '4'));

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!