共计 478 个字符,预计需要花费 2 分钟才能阅读完成。
数组数据
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"}
]
办法转换
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
});
});
正文完