要将给定的对象数组按照 field 属性进行分组
const data = [
{"name":"a","field":"f"},
{"name":"b","field":"ff"},
{"name":"v","field":"f"},
{"name":"d","field":"ff"},
];
// 按照field分组
groupArrayByField1(data) {
const groupedData = data.reduce((acc, obj) => {
const found = acc.find((item) => item.field === obj.field);
if (found) {
found.list.push(obj);
} else {
acc.push({ field: obj.field, list: [obj] });
}
return acc;
}, []);
return groupedData;
},