3

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

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. 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 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. Commented Jul 25, 2014 at 10:50
  • Yes but the resulting object needs to contain a list of all of its parents 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 Commented Jul 25, 2014 at 10:58

1 Answer 1

13

Here is a way to loop through the array recursively:

http://jsfiddle.net/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 comments, I'm a bit confused if you need anything else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.