I have an array of objects that represents a nested navigation list.
[
{
name: 'one',
link: 'blah/blah',
pages: [
{
name: 'one A'
link: 'blah/blah',
pages: []
},
{
name: 'one B'
link: 'blah/blah',
pages: []
},
{
name: 'one C'
link: null,
pages: [
{
name: 'one C I'
link: 'blah/blah',
pages: []
}
]
}
]
}
]
The first level of objects can have a link and pages the nested objects will either have a link or pages. I can't assume a limit to the depth of the nesting. I need an object for each state that includes its name, its link if it exists and all of its parents. My current solution does not account for deeper than 3 levels of nesting and adding support for each layer is laborious.
I also need to be able to search the resulting array of objects to get their link later on if that makes a difference to the solution.
I need a javascript solution but can also (and would like to) use the functions contained in the lodash library
I have an array of objects that represents a nested navigation list.
[
{
name: 'one',
link: 'blah/blah',
pages: [
{
name: 'one A'
link: 'blah/blah',
pages: []
},
{
name: 'one B'
link: 'blah/blah',
pages: []
},
{
name: 'one C'
link: null,
pages: [
{
name: 'one C I'
link: 'blah/blah',
pages: []
}
]
}
]
}
]
The first level of objects can have a link and pages the nested objects will either have a link or pages. I can't assume a limit to the depth of the nesting. I need an object for each state that includes its name, its link if it exists and all of its parents. My current solution does not account for deeper than 3 levels of nesting and adding support for each layer is laborious.
I also need to be able to search the resulting array of objects to get their link later on if that makes a difference to the solution.
I need a javascript solution but can also (and would like to) use the functions contained in the lodash library
Share Improve this question asked Jul 25, 2014 at 10:32 MarkMark 3,1976 gold badges40 silver badges78 bronze badges 5- 1 I think that the best would be to write a recursive function that takes each nested list as a parameter. Call the function again if the nested list is not empty. – Trace Commented Jul 25, 2014 at 10:45
- thanks I figured that, I am not really sure how to keep track of all the parents while iterating though – Mark Commented Jul 25, 2014 at 10:47
- Not sure what you mean. If you just loop through the top level, then call the same function on the sub levels, you will get through every 'pages' property that's in the list. – Trace Commented Jul 25, 2014 at 10:50
- Yes but the resulting object needs to contain a list of all of its parents – Mark Commented Jul 25, 2014 at 10:56
- for example the object that is created for 'one C I', needs a property that contains 'one C' and 'one' as they are its parents – Mark Commented Jul 25, 2014 at 10:58
1 Answer
Reset to default 13Here is a way to loop through the array recursively:
http://jsfiddle/yLnZe/37/
var arrPages = [{
name: 'one',
link: 'blah/blah',
pages: [{
name: 'one A',
link: 'blah/blah',
pages: []
},
{
name: 'one B',
link: 'blah/blah',
pages: []
},
{
name: 'one C',
link: null,
pages: [{
name: 'one C I',
link: 'blah blah',
pages: []
}]
}]
}];
function recursiveFunction(collection){
_.each(collection, function(model){
console.log(model);
if(model.pages.length > 0){
recursiveFunction(model.pages);
}
});
};
recursiveFunction(arrPages);
Is this what you need, or do you need anything more specifically? Based on your last ments, I'm a bit confused if you need anything else.