Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through multidimensional array with all unique IDs

I have a multidimensional array but the ID's are unique across parents and children, so I have a problem looping through using a for loop. The problem is that I cannot seem to grab the ID of the children. How do you think I should handle this?

    var Options = [
            {
                id: 0,
                children: []
            },
            {
                id: 2,
                children: []
            },
            {
                id: 3,
                children: [
                    {
                        id: 4,
                        children: []
                    },
                    {
                        id: 5,
                        children: []
                    },
                    {
                        id: 6,
                        children: []
                    }
                ]
            },
            {
                id: 7,
                children: [
                    {
                        id: 8,
                        children: []
                    },
                    {
                        id: 9,
                        children: []
                    }
                    ]
            }
        ];

I have kept the code concise for the sake of brevity. What I am trying to do is iterate through the array to compare ID's.

like image 890
Chris Bier Avatar asked Feb 22 '26 10:02

Chris Bier


2 Answers

This does not look like a "multidimensional array", but rather like a tree. Looping one level can be done with a simple for-loop:

for (var i=0; i<Options.length; i++) // do something

To loop the tree in-order, you will need a recursive function:

function loop (children, callback) {
    for (var i=0; i<children.length; i++) {
        callback(children[i]);
        loop(children[i].children, callback);
    }
}
loop(Options, console.log);

To get all children by their id, so that you can loop through the ids (regardless of the tree structure), use a lookup table:

var nodesById = {};
loop(Options, function(node) {
    nodesById[node.id] = node;
});
// access:
nodesById[4];

…and to loop them sorted by id, you now can do

Object.keys(nodesById).sort(function(a,b){return a-b;}).forEach(function(id) {
    var node = nodesById[id];
    // do something
});
like image 141
Bergi Avatar answered Feb 24 '26 00:02

Bergi


How about recursion?

var findById = function (arr, id) {
    var i, l, c;
    for (i = 0, l = arr.length; i < l; i++) {
        if (arr[i].id === id) {
            return arr[i];
        }
        else {
            c = findById(arr[i].children, id);
            if (c !== null) {
                return c;
            }
        }
    }
    return null;
}

findById(Options, 8);
like image 31
brianpeiris Avatar answered Feb 23 '26 23:02

brianpeiris



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!