如何对深层嵌套对象进行分类?

时间:2017-05-01 13:33:54

标签: javascript arrays json loops filter

我的反对意见如下:

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:[]
            }]
        }]
    }
    ]

我想在视图中列出对象。

enter image description here

JSON将在每个节点中深入到几个步骤,可能是6到8 children。 我无法在javaScript中找到合适的方法。

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

1 个答案:

答案 0 :(得分:0)

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

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数组传递给它以执行相同的操作。

您可以在下面的代码段中查看一个工作示例。



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)