Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting/navigating nested JavaScript object/array

Tags:

javascript

I've got this data:

const items = [
  {
    _id: 0,
    content: 'Item 1 something',
    note: 'Some note for item 1'
  },
  {
    _id: 5,
    content: 'Item 1.1 something',
    note: 'Some note for item 1.1'
  },
  {
    _id: 1,
    content: 'Item 2 something',
    note: 'Some note for item 2',
    subItems: [
      {
        _id: 2,
        parent_id: 1,
        content: 'Sub Item 1 something',
        subItems: [{
          _id: 3,
          parent_id: 2,
          content: 'Sub Sub Item 4'
        }]
      }
    ]
  }
];

Using Javascript, how can I navigate/insert into the tree, provided at any point I have the _id of one item in the tree.

For example, some case scenarios:

  • I am at _id 3 and want to insert another sibling to _id 3
  • I am at _id 2 and want to go up to _id 1 - how do I get _id 1?
  • I am at _id 5 and want to go to _id 1

How do I navigate the tree using only an _id?

like image 742
Hirvesh Avatar asked Apr 21 '26 01:04

Hirvesh


1 Answers

You could iterate the array and test if _id property has the wanted value. Then save either the node, the parent or the next item of the array.

For getting the parent node, the actual parent is saved as a closure and returned if the wanted _id is found.

All functions test for subItems as array and if, it performs an iteration over subItems.

function getNode(array, id) {
    var node;
    array.some(function iter(a) {
        if (a._id === id) {
            node = a;
            return true;
        }
        return Array.isArray(a.subItems) && a.subItems.some(iter);
    });
    return node;
}

function getParent(array, id) {
    var parent ;
    array.some(function iter(p) {
        return function (a) {
            if (a._id === id) {
                parent = p;
                return true;
            }
            return Array.isArray(a.subItems) && a.subItems.some(iter(a));
        };
    }(undefined));
    return parent;
}

function getNextNode(array, id) {
    var node;
    array.some(function iter(a, i, aa) {
        if (a._id === id) {
            node = aa[i + 1];
            return true;
        }
        return Array.isArray(a.subItems) && a.subItems.some(iter);
    });
    return node;
}

var items = [{ _id: 0, content: 'Item 1 something', note: 'Some note for item 1' }, { _id: 5, content: 'Item 1.1 something', note: 'Some note for item 1.1' }, { _id: 1, content: 'Item 2 something', note: 'Some note for item 2', subItems: [{ _id: 2, parent_id: 1, content: 'Sub Item 1 something', subItems: [{ _id: 3, parent_id: 2, content: 'Sub Sub Item 4' }] }] }];

console.log(getNode(items, 3));
console.log(getParent(items, 2));
console.log(getNextNode(items, 5));
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 135
Nina Scholz Avatar answered Apr 23 '26 16:04

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!