数组数据

data = [
    {name: "Z", detail: "zzz"},
    {name: "W", detail: "wwww1"},
    {name: "W", detail: "www2"},
    {name: "W", detail: "www3"},
    {name: "F", detail: "ff1"},
    {name: "F", detail: "ff2"},
    {name: "L", detail: "ll2"},
    {name: "L", detail: "ll2"}
  ]

方法转换1

let obj = {};
data.forEach((item, index) => {
    let { name } = item;
    if (!obj[name]) {
        obj[name] = {
            name,
            children: []
        }
    }
    obj[name].children.push(item);
});
let dataPush = Object.values(obj); // 最终输出

方法转换2

let listArr = [];
data.forEach(function (val, index) {
  for (var i = 0; i < listArr.length; i++) {
      // 对比相同的字段key,相同放入对应的数组
      if (listArr[i].name == val.name) {
          listArr[i].features.push({
              name: val.name,
              detail: val.detail
           });
           return;
      }
  }
  // 第一次对比没有参照,放入参照
  listArr.push({
      name: val.name,
      detail: val.detail
  });
});

万一呢
39 声望1 粉丝

努力学习进阶的小白一枚