示例代码
const repeatId = [
{
id: 1,
count: 12,
},
{
id: 1,
count: 15,
},
{
id: 2,
count: 20,
},
{
id: 3,
count: 25,
},
{
id: 2,
count: 30,
},
];
const obj = {};
repeatId.forEach((item) => {
if (!Array.isArray(obj[item.id])) {
obj[item.id] = [];
obj[item.id].push(item);
} else {
obj[item.id].push(item);
}
});
console.log(obj);
结果



8333

被折叠的 条评论
为什么被折叠?



