I have an array of objects like below for example.
{name: "Mc Donald", quantity: 4, maleCount: 1, femaleCount: 0}
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0}
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0}
{name: "Mc Donald", quantity: 4, maleCount: 0, femaleCount: 1}
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1}
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0}
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1}
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1}
I want to group them up and adding up all the values in it. For example, the final would be like this:
{name: "Mc Donald", quantity: 8, maleCount: 1, femaleCount: 1}
{name: "KFC", quantity: 54, maleCount: 3, femaleCount: 3}
How can I achieve this in JavaScript?
I have tried to find some solution online but it is not the one I wanted. For example this solution
The group() method groups the elements of the calling array according to the string values returned by a provided testing function. The returned object has separate properties for each group, containing arrays with the elements in the group.
The most efficient method to group by a key on an array of objects in js is to use the reduce function. The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
To combine two arrays into an array of objects, use map() from JavaScript.
The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.
You can use array reduce:
var arr = [
{name: "Mc Donald", quantity: 4, maleCount: 1, femaleCount: 0},
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0},
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0},
{name: "Mc Donald", quantity: 4, maleCount: 0, femaleCount: 1},
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1},
{name: "KFC", quantity: 9, maleCount: 1, femaleCount: 0},
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1},
{name: "KFC", quantity: 9, maleCount: 0, femaleCount: 1}
];
var finalArr = arr.reduce((m, o) => {
var found = m.find(p => p.name === o.name);
if (found) {
found.quantity += o.quantity;
found.maleCount += o.maleCount;
found.femaleCount += o.femaleCount;
} else {
m.push(o);
}
return m;
}, []);
console.log(finalArr);
You can use forEach()
loop and add to new array. You can pass empty object as thisArg
parameter that you can use as this
in your callback function and in second forEach context of this
will still be the same as in first callback function because of arrow function.
var data = [{"name":"Mc Donald","quantity":4,"maleCount":1,"femaleCount":0},{"name":"KFC","quantity":9,"maleCount":1,"femaleCount":0},{"name":"KFC","quantity":9,"maleCount":1,"femaleCount":0},{"name":"Mc Donald","quantity":4,"maleCount":0,"femaleCount":1},{"name":"KFC","quantity":9,"maleCount":0,"femaleCount":1},{"name":"KFC","quantity":9,"maleCount":1,"femaleCount":0},{"name":"KFC","quantity":9,"maleCount":0,"femaleCount":1},{"name":"KFC","quantity":9,"maleCount":0,"femaleCount":1}]
var result = [], keys = ['quantity', 'maleCount', 'femaleCount']
data.forEach(function(e) {
if(!this[e.name]) result.push(this[e.name] = e);
else keys.forEach(k => this[e.name][k] += e[k])
}, {})
console.log(result)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With