Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group or merge this array of objects in javascript?

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

like image 553
the newbie coder Avatar asked Oct 10 '17 10:10

the newbie coder


People also ask

How do you group an array in JavaScript?

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.

How do you group objects in an array?

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.

How do I combine two arrays of objects?

To combine two arrays into an array of objects, use map() from JavaScript.

How do I merge one array to another in 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.


2 Answers

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);
like image 150
Faly Avatar answered Sep 22 '22 09:09

Faly


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)
like image 43
Nenad Vracar Avatar answered Sep 22 '22 09:09

Nenad Vracar