const arr = [
{
htPid: 1, name: "A"
}, {
htPid: 2, name: "B"
}, {
htPid: 3, name: "C"
}]
//1.reduce
const groupByCategory = arr.reduce((group, product, j) => {
const { htPid } = product;
group[htPid] = group[htPid] ?? [];
group[htPid].push(product);
return group;
}, {});
console.log('groupByCategory', groupByCategory)
//2.groupBy
const peopleByAge = Object.groupBy(arr, (person) => person.name);
console.log('peopleByAge', peopleByAge)
交流V:MuskYu_