使用 javascript/lodash 迭代嵌套对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24953697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:39:32  来源:igfitidea点击:

iterating through array of nested objects with javascript/lodash

javascriptalgorithmlodash

提问by Mark

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.

第一级对象可以有一个链接和页面,嵌套对象将有一个链接或页面。我不能假设嵌套深度有限制。我需要每个状态的对象,其中包括其名称、链接(如果存在)及其所有父项。我目前的解决方案没有考虑到 3 层以上的嵌套,并且为每一层添加支持很费力。

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

我需要一个 javascript 解决方案,但也可以(并且想要)使用 lodash 库中包含的函数

回答by Trace

Here is a way to loop through the array recursively:

这是一种递归遍历数组的方法:

http://jsfiddle.net/yLnZe/37/

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.

这是您需要的,还是您需要更具体的东西?根据您最后的评论,如果您还需要其他任何东西,我有点困惑。