1

I have an array of objects that looks like this:

[
 {1: 22},
 {1: 56},
 {2: 345},
 {3: 23},
 {2: 12}
]

I need to make it look like this:

[{1: 78}, {2: 357}, {3: 23}]

Is there a way I could make it so that it can sum up all the values that have the same key? I have tried using a for each loop but that hasn't helped at all. I would really appreciate some help. Thank you!

Parth Patel
  • 63
  • 1
  • 2
  • 5

2 Answers2

1

You can use reduce for this to build up a new object. You start with an empty object and set the key to the value from the original array or add it to an existing object already. Then to get an array, just map it back.

let arr = [{1: 22},{1: 56},{2: 345},{3: 23},{2: 12}];

let tot = arr.reduce((a,obj) => {
    let [k, v] = Object.entries(obj)[0]
    a[k] = (a[k] || 0) + v
    return a
}, {})

let final = Object.entries(tot).map(([k,v]) => {
    return {[k]:v}
})
console.log(final);
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    This worked for me! Thank you so much! I was trying to use Object.keys instead of Object.entries and was confused about how I could reference keys of objects when they are numbers. – Parth Patel Apr 12 '18 at 22:22
0

You can use reduce to make an object of the sum, then convert that object to an array of objects:

function group(arr) {
    var sumObj = arr.reduce(function(acc, obj) {
        var key = Object.keys(obj)[0];                  // get the key of the current object (assuming there is only one)
        if(acc.hasOwnProperty(key)) {                   // if there is an entry of that object in acc
            acc[key] += obj[key];                       // add to it the current object's value
        } else {
            acc[key] = obj[key];                        // otherwise, create a new entry that initially contains the current object's value
        }
        return acc;
    }, {});

    return Object.keys(sumObj).map(function(key) {      // now map each key in sumObj into an individual object and return the resulting objects as an array
        return { [key]: sumObj[key] };
    });
}

Example:

function group(arr) {
    var sumObj = arr.reduce(function(acc, obj) {
        var key = Object.keys(obj)[0];                  // get the key of the current object (assuming there is only one)
        if(acc.hasOwnProperty(key)) {                   // if there is an entry of that object in acc
            acc[key] += obj[key];                       // add to it the current object's value
        } else {
            acc[key] = obj[key];                        // otherwise, create a new entry that initially contains the current object's value
        }
        return acc;
    }, {});

    return Object.keys(sumObj).map(function(key) {      // now map each key in sumObj into an individual object and return the resulting objects as an array
        return { [key]: sumObj[key] };
    });
}

var arr = [ {1: 22}, {1: 56}, {2: 345}, {3: 23}, {2: 12} ];
console.log(group(arr));
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73