[{vId: 1, aId: 1},{vId: 1, aId: 2},{vId: 2, aId: 1}] 如何 根据vId相同转变成 [{vId:1, aIds:[1,2]},{vId:2, aIds:[1]}]
const demo = [{ vId: 1, aId: 1 }, { vId: 1, aId: 2 }, { vId: 2, aId: 1 }];
demo
.map(item => {
const { vId, aId } = item;
return {
vId,
aId: [aId]
};
})
.reduce((acc, item) => {
if (acc.length === 0) {
acc.push(item);
return acc;
} else {
acc.map(_item => {
if (_item.vId === item.vId) {
_item.aId = [..._item.aId, ...item.aId];
}
return _item;
});
return acc;
}
}, []);
console.log(demo);