取数组属性值相同数据 按照属性值分类组成新数组

数组数据

data = https://www.it610.com/article/[ {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 }); });

    推荐阅读