How to group or merge this array of objects in jav

2020-02-13 07:46发布

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

8条回答
够拽才男人
2楼-- · 2020-02-13 08:07

My solution based on reduce:

let 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}
];

// We will only keep an array of values of the merged object
let result = Object.values(
  // Merge data to name indexed objects
  data.reduce((p,c) => {
    // Ensure key (name) exists
    if (!p[c.name]) {
       // If first occurence, duplicate object (to avoid modification of original array)
       p[c.name] = Object.assign({}, c);
    } else {
       // If key (name) already exists, sum up relevant attributes
       p[c.name].quantity += c.quantity;
       p[c.name].maleCount += c.maleCount;
       p[c.name].femaleCount += c.femaleCount;
    }
    // return updated object
    return p;
  }, {})
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

hope this will help

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-13 08:08

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);

查看更多
啃猪蹄的小仙女
4楼-- · 2020-02-13 08:08

check this code below:

var array = [
    {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 = [];

for(var index in array){
    var obj = array[index];
    if(result.hasOwnProperty(obj.name)) {
        result[obj.name].quantity += obj.quantity;
        result[obj.name].maleCount += obj.maleCount;
        result[obj.name].femaleCount += obj.femaleCount;
    }else{
        result[obj.name] = {};
        result[obj.name].quantity = obj.quantity;
        result[obj.name].maleCount = obj.maleCount;
        result[obj.name].femaleCount = obj.femaleCount;
    }
}

console.log(result);
查看更多
在下西门庆
5楼-- · 2020-02-13 08:11

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)

查看更多
一夜七次
6楼-- · 2020-02-13 08:15

Here is a generic solution that uses clean and modern JavaScript:

function groupRestaurants(restaurants) {
    const groups = new Map();
    restaurants.forEach(restaurant => {
        const group = findOrCreateGroup(groups, restaurant.name);
        addRestaurantToGroup(group, restaurant);
    });
    return [...groups.values()];
}

function findOrCreateGroup(groups, name) {
    let group = groups.get(name);
    if (!group) {
        group = { name };
        groups.set(name, group);
    }
    return group;
}

function addRestaurantToGroup(group, restaurant) {
    for (const [key, value] of Object.entries(restaurant)) {
        if (key !== 'name') {
            if (group.hasOwnProperty(key)) {
                group[key] += value;
            } else {
                group[key] = value;
            }
        }
    }
}

const restaurants = [
    { 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 }
];

console.log(groupRestaurants(restaurants));

查看更多
Juvenile、少年°
7楼-- · 2020-02-13 08:16

You could group with a hash table and use an array for the keys with variable values.

var array = [{ 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 }],
    groups = Object.create(null),
    result = array.reduce(function (r, o) {
        var values = ['quantity', 'maleCount', 'femaleCount'];
        if (!groups[o.name]) {
            groups[o.name] = { name: o.name };
            r.push(groups[o.name]);
            values.forEach(function (k) { groups[o.name][k] = 0; });
        }
        values.forEach(function (k) { groups[o.name][k] += o[k]; });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

With linq.js

var array = [{ 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 }],
    result = Enumerable.From(array)
        .GroupBy(
            "$.name",
            null,
            "{ name: $.Key, quantity: $$.Sum('$.quantity'), maleCount: $$.Sum('$.maleCount'), femaleCount: $$.Sum('$.femaleCount') }"
        )
        .ToArray();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

查看更多
登录 后发表回答