【问题标题】:How to categorize deep nested object?如何对深层嵌套对象进行分类?
【发布时间】:2017-05-01 13:33:54
【问题描述】:

我有这样的反对:

var list = [
    {
        category:'CATEGORY 1',
        label:'Item 1',
        children:[{
            category:'CATEGORY 2',
            label:'Item 1',
            children:[]
        },{
            category:'CATEGORY 2',
            label:'Item 2',
            children:[{
                category:'CATEGORY 3',
                label:'Item 1',
                children:[]
            },{
                category:'CATEGORY 3',
                label:'Item 2',
                children:[]
            }]
        }]
    },
    {
        category:'CATEGORY 1',
        label:'Item 2',
        children:[{
            category:'CATEGORY 2',
            label:'Item 3',
            children:[]
        },{
            category:'CATEGORY 2',
            label:'Item 4',
            children:[{
                category:'CATEGORY 3',
                label:'Item 2',
                children:[]
            },{
                category:'CATEGORY 3',
                label:'Item 3',
                children:[]
            }]
        }]
    }
    ]

我想像在视图中一样列出对象。

JSON 将深入到几个步骤,每个节点中可能有 6 到 8 个children。 我无法在 javaScript 中找到合适的方法来执行此操作。

我是否需要将每个类别分开并循环遍历每个对象?

【问题讨论】:

    标签: javascript arrays json loops filter


    【解决方案1】:

    看起来你需要一个递归函数来帮助你。看看这个:

    function findCategories(list) {
      list.forEach(function(item) {
        // do something with the category and label here
        console.log(item.category);
    
        // does this object have any children? if yes, call find categories again
        if (item.hasOwnProperty("children")) {
          findCategories(item.children);
        }
      })
    }
    

    此函数将遍历您的所有类别,并检查是否有 children 属性。如果有,我们将再次调用findCategories() 函数并将children 数组传递给它以执行相同的操作。

    您可以在下面的 sn-p 中查看一个工作示例。

    var list = [
        {
            category:'CATEGORY 1',
            label:'Item 1',
            children:[{
                category:'CATEGORY 2',
                label:'Item 1',
                children:[]
            },{
                category:'CATEGORY 2',
                label:'Item 2',
                children:[{
                    category:'CATEGORY 3',
                    label:'Item 1',
                    children:[]
                },{
                    category:'CATEGORY 3',
                    label:'Item 2',
                    children:[]
                }]
            }]
        },
        {
            category:'CATEGORY 1',
            label:'Item 2',
            children:[{
                category:'CATEGORY 2',
                label:'Item 3',
                children:[]
            },{
                category:'CATEGORY 2',
                label:'Item 4',
                children:[{
                    category:'CATEGORY 3',
                    label:'Item 2',
                    children:[]
                },{
                    category:'CATEGORY 3',
                    label:'Item 3',
                    children:[]
                }]
            }]
        }
        ]
    
    function findCategories(list) {
      list.forEach(function(item) {
        // do something with the category and label here
        console.log(item.category);
    
        // does this object have any children? if yes, call find categories again
        if (item.hasOwnProperty("children")) {
          findCategories(item.children);
        }
      })
    }
    
    findCategories(list)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2020-01-30
      • 2017-12-15
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      相关资源
      最近更新 更多