Home > Enterprise >  Get the branch of collection without the sibling elements, searching by property
Get the branch of collection without the sibling elements, searching by property

Time:09-17

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');

CodePudding user response:

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'));

  • Related