I have a very complex multidimensional array.
Each item is like (this code shows the var that is pushed to the array):
var arrayItem = {
'itemId': '00001',
'itemName': 'Biscuits',
'parentId': 'Food',
'children': []
};
itemId is always unique. The children array is populated with more arrayItem(s). The array can therefore have many levels (possible max 9, but maybe more) – So the base array has x number of objects and each of those can have y number of children objects and so on…
Is there a way to search for a particular itemId. I have tried various .each methods but I can’t seem to search all of the levels of the multidimentional array.
Any help is much appreciated.
Edit:
A few years worth of experience and
Arrayprototype improvements later, here’s how I’d solve the problem:This performs a breadth-first search through the object graph, and returns
undefinedif it does not contain the ID you’re looking for.Original answer:
Pass finder your array and the desired ID, and it will find and return the array item with that id.
Use it like this:
Demo here: http://jsfiddle.net/mVyha/