How to sum object values by a key value that is an date object?
I try to sum all "Menge" and "Fehler" values if the "Datum" is the same. The probelmm is that "Datum" is a date object.
var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]
console.log(arr)
var holder = {};
arr.forEach(function (d) {
if (holder.hasOwnProperty(d.Datum)) {
holder[d.Datum] = holder[d.Datum] + d.Menge;
} else {
holder[d.Datum] = d.Menge;
}
});
var obj2 = [];
for (var prop in holder) {
obj2.push({
Datum: prop,
Menge: holder[prop]
});
}
console.log(obj2);
But the result should be:
[
{
"Datum": "2000-01-01",
"Menge": 121,
"Fehler": 76
},
{
"Datum": "2000-01-02",
"Menge": 30,
"Fehler": 2
}
]
Solution 1:
Kind of ES6 way and grouping by hash solution:
const arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]
const result = Object.values(arr.reduce((acc, obj) => {
const [Datum] = obj.Datum.date.split(' ');
const Menge = (acc[Datum]?.Menge + obj.Menge) || obj.Menge;
const Fehler = (acc[Datum]?.Fehler + obj.Fehler) || obj.Fehler;
const Ratio = Fehler / Menge;
acc[Datum] = { Datum, Menge, Fehler, Ratio };
return acc;
}, {}));
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
Solution 2:
If you ignore the timezone and the time part, you can just extract the date part as below and use it as the key for holder
.
var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]
var holder = {};
arr.forEach(function (d) {
const dateStr = d.Datum.date.substring(0, 10);
if (holder.hasOwnProperty(dateStr)) {
var existing = holder[dateStr];
holder[dateStr] = {
Datum: dateStr,
Menge: existing.Menge + d.Menge,
Fehler: existing.Fehler + d.Fehler,
};
} else {
holder[dateStr] = {
Datum: dateStr,
Menge: d.Menge,
Fehler: d.Fehler,
};
}
});
var obj2 = Object.values(holder);
console.log(obj2);
Solution 3:
Alternative way;
var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]
var Arrr = [];
arr.forEach(function (d) {
var dateArr = d.Datum.date.split(" ");
if(Arrr.find(x=> x.Datum ===dateArr[0]))
{
Arrr.find(x=> x.Datum ===dateArr[0]).Fehler += d.Fehler;
Arrr.find(x=> x.Datum ===dateArr[0]).Menge += d.Menge;
}
else {
Arrr.push({
Datum: dateArr[0],
Menge: d.Menge,
Fehler:d.Fehler
});
}
});
console.log(Arrr);