合并对象数组而不覆盖

时间:2019-02-04 15:19:58

标签: javascript json ecmascript-6 treeview

我当前使用的是JSON,必须以树状层次结构进行构造。层次结构的深度变化很大,因此是未知的。

目前,我已经获得了一系列对象。示例如下。

[
    {
      "name": "level1",
      "collapsed": true,
      "children": [
        {
          "name": "Level 1 item here",
          "id": 360082134191
        }
      ]
    },
    {
      "name": "level1",
      "collapsed": true,
      "children": [
        {
          "name": "level2",
          "collapsed": true,
          "children": [
            {
              "name": "Level 2 item here",
              "id": 360082134751
            }
          ]
        }
      ]
    },
    {
      "name": "level1",
      "collapsed": true,
      "children": [
        {
          "name": "Another level 1 item",
          "id": 360082262772
        }
      ]
    }
  ]

我要实现的目标是合并这些对象,而不覆盖或替换任何对象。下面列出的是我想要如何格式化数据的示例:

[
  {
    "name": "level1",
    "collapsed": true,
    "children": [
      {
        "name": "level2",
        "collapsed": true,
        "children": [
          {
            "name": "Level 2 item here",
            "id": 360082134751
          }
        ]
      },
      {
        "name": "Level 1 item here",
        "id": 360082134191
      },
      {
        "name": "Another level 1 item",
        "id": 360082262772
      }
    ]
  }
]

如何使用JavaScript实现此目标?没有库是首选,但是可以使用ES6。

编辑: 输出是数组很重要,因为没有子项的项可以出现在根目录中。

2 个答案:

答案 0 :(得分:0)

我假设您在处理数据方面需要一点帮助。可能有多种方法可以实现这一目标,这就是我的方法。

// data => supplied data
const result = data.reduce ((acc, item) => {
  // if acc array already contains an object with same name,
  // as current element [item], merfe the children
  let existingItem;

  // Using a for loop here to create a reference to the
  // existing item, so it'd update this item when childrens
  // will be merged.
  for (let index = 0; index < acc.length; index ++) {
    if (acc[index].name === item.name) {
      existingItem = acc[index];
      break;
    }
  }
  // if existingItem exists, merge children of
  // existing item and current item.
  // else push it into the accumulator
  if (existingItem) {
    existingItem.children = existingItem.children.concat(item.children);
  } else {
    acc.push (item);
  }
  return acc;
}, []);

答案 1 :(得分:0)

我假设您要基于1级对象中的name属性进行分组。您可以像这样做一个简单的reduceObject.values

const input = [{"name":"level1","collapsed":true,"children":[{"name":"Level 1 item here","id":360082134191}]},{"name":"level1","collapsed":true,"children":[{"name":"level2","collapsed":true,"children":[{"name":"Level 2 item here","id":360082134751}]}]},{"name":"level1","collapsed":true,"children":[{"name":"Another level 1 item","id":360082262772}]}]
  
const merged = input.reduce((r,{name, collapsed, children}) =>{
    r[name] = r[name] || {name, collapsed, children:[]};
    r[name]["children"].push(...children)
    return r;
}, {})
  
const final = Object.values(merged);
console.log(final)

您可以在一行中完成全部操作:

const input = [{"name":"level1","collapsed":true,"children":[{"name":"Level 1 item here","id":360082134191}]},{"name":"level1","collapsed":true,"children":[{"name":"level2","collapsed":true,"children":[{"name":"Level 2 item here","id":360082134751}]}]},{"name":"level1","collapsed":true,"children":[{"name":"Another level 1 item","id":360082262772}]}]

const output = Object.values(input.reduce((r,{name,collapsed,children}) => (
    (r[name] = r[name] || {name,collapsed,children: []})["children"].push(...children), r), {}))
console.log(output)