我正在尝试使用reduce方法对对象数组进行排序。我的目标是创建一个reduce方法,它可以处理任意数量的对象(具有唯一的eventIds)。期望的输出格式是这样的,但是能够处理多于五个唯一的事件id
(期望输出值)
(5) [{…}, {…}, {…}, {…}, {…}]
0:
eventId: "12345"
total: Array(3)
0: {total: 123}
1: {total: 45}
2: {total: 123}
length: 3
1: {total: Array(3), eventId: '12347'}
2: {total: Array(3), eventId: '12349'}
3: {total: Array(3), eventId: '12348'}
4: {total: Array(3), eventId: '12346'}
为了得到上面的输出,我使用了这个reduce方法。
[减少方法]
const sortedSalesData = salesData.reduce((acc, obj) => {
for ( let i = 0; i<salesData.length; i++) {
if (obj.eventId === salesData[i].eventId ) {
acc[i].eventId = salesData[i].eventId;
acc[i].total = [...acc[i].total, {total: obj.total}];
return acc;
}
};
},[{total:[]},{total:[]},{total:[]},{total:[]},{total:[]}]);
但是我不确定如何设置它来处理多于这五个唯一的事件id。下面是原始数组供参考。
(原始数组)
const salesData =
[
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12348', total: 123 },
{ eventId: '12346', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12347', total: 123 },
{ eventId: '12345', total: 45 },
{ eventId: '12348', total: 78 },
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12346', total: 78 },
{ eventId: '12349', total: 123 },
{ eventId: '12348', total: 45 },
{ eventId: '12346', total: 78 },
];
挂起是让它可以处理任意数量的唯一事件,而不必在方法末尾添加额外的{total:[]}。
你想迭代初始数组(obj),插入迭代元素到一个新的数组(acc)如果eventId还不存在的数组,当它存在(accObj),你想把当前元素(obj)的总对象推到新元素(accObj)的总数组?
我认为这正是你所需要的:
let array = [
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12348', total: 123 },
{ eventId: '12346', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12347', total: 123 },
{ eventId: '12345', total: 45 },
{ eventId: '12348', total: 78 },
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12346', total: 78 },
{ eventId: '12349', total: 123 },
{ eventId: '12348', total: 45 },
{ eventId: '12346', total: 78 },
]
let reducedArray = array.reduce((acc, obj) => {
console.log("Element " + JSON.stringify(obj));
let accObj = acc.find(accObj => accObj.eventId === obj.eventId);
if(accObj != undefined) {
console.log("Found element: " + JSON.stringify(accObj) + ".")
console.log("Pushing "+ obj.total +" to total.")
accObj.total.push(obj.total);
} else {
accObj = { eventId: obj.eventId, total: [obj.total] };
console.log("Pushing new element to list: " + JSON.stringify(accObj));
acc.push(accObj);
}
return acc;
}, []);
console.log(JSON.stringify(reducedArray));
技巧是在find
方法上,我正在搜索我自己的缩减列表,看看它是否已经有一个与旧数组共享eventId的元素。
我不喜欢。reduce,所以这里有一个简单的解决方案,没有它
const salesData =
[
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12348', total: 123 },
{ eventId: '12346', total: 45 },
{ eventId: '12349', total: 78 },
{ eventId: '12347', total: 123 },
{ eventId: '12345', total: 45 },
{ eventId: '12348', total: 78 },
{ eventId: '12345', total: 123 },
{ eventId: '12347', total: 45 },
{ eventId: '12346', total: 78 },
{ eventId: '12349', total: 123 },
{ eventId: '12348', total: 45 },
{ eventId: '12346', total: 78 },
]
let reduced = {}
salesData.forEach(elem=> {
if(reduced[elem.eventId] !== undefined){
reduced[elem.eventId].push(elem.total)
} else {
reduced[elem.eventId] = [elem.total]
}
})
reduced = Object.keys(reduced).map(elem => {
return({eventId: elem, total:reduced[elem]})
})
console.log(reduced)