Typescript minska objektsarray efter specifikt värde
Jag har detta:
0: {orderType: "orderType1", orderCount: 0, orderDate: 47}
1: {orderType: "orderType1", orderCount: 21, orderDate: 47}
2: {orderType: "orderType1", orderCount: 3, orderDate: 47}
3: {orderType: "orderType1", orderCount: 5, orderDate: 48}
4: {orderType: "orderType1", orderCount: 32, orderDate: 48}
5: {orderType: "orderType1", orderCount: 12, orderDate: 48}
och jag skulle vilja uppnå detta:
0: {orderType: "orderType1", orderCount: 24, orderDate: 47}
1: {orderType: "orderType1", orderCount: 49, orderDate: 48}
I princip vill jag 'kombinera' eller 'reducera' posterna genom att kombinera orderCount baserat på orderDate.
Ursäkta om kombinera eller minska inte är rätt termer här.
Svar #1
Någon smartare än jag kan förmodligen göra detta mer kortfattat, men om jag grupperar datan och sedan reducerar den kan jag få önskat resultat.
Jag har en känsla av att detta kan göras i färre steg:
function setFilter(value, index, self) {
return self.indexOf(value) === index;
}
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
var key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
const data = [
{ orderType: "orderType1", orderCount: 0, orderDate: 47 },
{ orderType: "orderType1", orderCount: 21, orderDate: 47 },
{ orderType: "orderType1", orderCount: 3, orderDate: 47 },
{ orderType: "orderType1", orderCount: 5, orderDate: 48 },
{ orderType: "orderType1", orderCount: 32, orderDate: 48 },
{ orderType: "orderType1", orderCount: 12, orderDate: 48 }
];
const groupedData = groupBy(data, 'orderDate');
const reducedData = [];
for (let key in groupedData) {
let initialValue = 0;
let sum = groupedData[key].reduce((accumulator, currentValue) => {
return accumulator + currentValue.orderCount;
},initialValue)
reducedData.push({
orderType: groupedData[key][0].orderType,
orderCount: sum,
orderDate: key
});
}
console.log(reducedData);
Det här tar inte hänsyn till orderType och jag tycker att det borde det.
Svar #2
Här är en (lite) kortare version som också använder reduce:
const data = [
{ orderType: "orderType1", orderCount: 0, orderDate: 47 },
{ orderType: "orderType1", orderCount: 21, orderDate: 47 },
{ orderType: "orderType1", orderCount: 3, orderDate: 47 },
{ orderType: "orderType1", orderCount: 5, orderDate: 48 },
{ orderType: "orderType1", orderCount: 32, orderDate: 48 },
{ orderType: "orderType1", orderCount: 12, orderDate: 48 }];
let foundIdx;
var result = data.reduce((accu, curr, idx, arr) => {
if (accu.some((value, resIdx, reArr) => { // present in out array?
foundIdx = resIdx;
return (value.orderType == curr.orderType && value.orderDate == curr.orderDate);
}))
{ // already present, increment OrderCount
accu[foundIdx].orderCount += curr.orderCount;
}
else { // not yet present push the element
accu.push(curr);
}
return accu;
}, []);
console.log(JSON.stringify(result));
//[{"orderType":"orderType1","orderCount":24,"orderDate":47},{"orderType":"orderType1","orderCount":49,"orderDate":48}]
