移动嵌套对象并从中构建一个扁平对象

时间:2017-09-24 20:00:22

标签: javascript arrays json

我有一个需要遍历的对象数组,随着深度的增加,我需要在每个节点上分配一个treeLevel属性。 我创建了以下plunker,https://plnkr.co/edit/wou4Vj?p=info 我已经找到了旅行部分,但我无法了解如何在每个节点上分配treeLevels。​​

sampleInput = [
    {
      sectorName: 'a',
      sectorType: 'a',
      sectors: [
        {
          sectorName: 'a-a1',
          sectorType: 'a-a1',
          sectors: [
            {
              sectorName: 'a-a1-a1',
              sectorType: 'a-a1-a1',  
            },
            {
              sectorName: 'a-a1-a2',
              sectorType: 'a-a1-a2',  
            },
            {
              sectorName: 'a-a1-a3',
              sectorType: 'a-a1-a3',
            }
          ]
        },
        {
          sectorName: 'a-a2',
          sectorType: 'a-a2',
        },
        {
          sectorName: 'a-a3',
          sectorType: 'a-a3',
          sectors: [
            {
              sectorName: 'a-a3-a1',
              sectorType: 'a-a3-a1',  
              sectors: [
                {
                  sectorName: 'a-a3-a1-a1',
                  sectorType: 'a-a3-a1-a1',
                }
              ]
            }
          ]
        }
      ]
    },
    {
      sectorName: 'b',
      sectorType: 'b',
    },
    {
      sectorName: 'c',
      sectorType: 'c',
      sectors: [
        {
            sectorName: 'c-c1',
            sectorType: 'c-c2',
        }
      ]
    }
  ];

以下是我期待的输出,

"[
  {"sectorName":"a","sectorType":"a","$$treeLevel":0},
  {"sectorName":"a-a1","sectorType":"a-a1","$$treeLevel":1},
  {"sectorName":"a-a1-a1","sectorType":"a-a1-a1","$$treeLevel":0},
  {"sectorName":"a-a1-a2","sectorType":"a-a1-a2","$$treeLevel":0},
  {"sectorName":"a-a1-a3","sectorType":"a-a1-a3","$$treeLevel":0},
  {"sectorName":"a-a2","sectorType":"a-a2","$$treeLevel":0},
  {"sectorName":"a-a3","sectorType":"a-a3","$$treeLevel":1},
  {"sectorName":"a-a3-a1","sectorType":"a-a3-a1","$$treeLevel":2},
  {"sectorName":"a-a3-a1-a1","sectorType":"a-a3-a1-a1","$$treeLevel":3},    
  {"sectorName":"b","sectorType":"b","$$treeLevel":0},
  {"sectorName":"c","sectorType":"c","$$treeLevel":0},
  {"sectorName":"c-c1","sectorType":"c-c2","$$treeLevel":1}
]"

增加treeLevel的逻辑是,如果我们将扇区数组作为当前对象的一部分,则复制当前对象,为其指定treeLevel,递增treeLevel,然后递归。 但请注意,同一父项的直接子项将被分配正确的树级别。例如,上面{"sectorName":"a-a3","sectorType":"a-a3","$$treeLevel":1},'a-a3'是'a-a1'的直接子节点,将指定treeLevel为1而不是零。

有人可以帮我解决这个问题。我只需要弄清楚如何使树级别的分配工作。我对所有解决方案,递归,非递归,使用lodash lib等开放。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以存储实际级别并在每个嵌套级别上递增它。

function getFlat(array) {
    var result = [];
    array.forEach(function flat(level) {
        return function (o) {
            result.push({ sectorName: o.sectorName, sectorType: o.sectorType, $$treeLevel: level });
            o.sectors && o.sectors.forEach(flat(level + 1));
        };
    }(0));
    return result;
}

var data = [{ sectorName: 'a', sectorType: 'a', sectors: [{ sectorName: 'a-a1', sectorType: 'a-a1', sectors: [{ sectorName: 'a-a1-a1', sectorType: 'a-a1-a1', }, { sectorName: 'a-a1-a2', sectorType: 'a-a1-a2', }, { sectorName: 'a-a1-a3', sectorType: 'a-a1-a3', }] }, { sectorName: 'a-a2', sectorType: 'a-a2', }, { sectorName: 'a-a3', sectorType: 'a-a3', sectors: [{ sectorName: 'a-a3-a1', sectorType: 'a-a3-a1', sectors: [{ sectorName: 'a-a3-a1-a1', sectorType: 'a-a3-a1-a1', }] }] }] }, { sectorName: 'b', sectorType: 'b', }, { sectorName: 'c', sectorType: 'c', sectors: [{ sectorName: 'c-c1', sectorType: 'c-c2', }] }],
    result = getFlat(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }