0

Below is input and output

//Input: [{a:2}, {b:3},{a:2},{b:3}]

    // Output: {

    //     a:4,

    //     b:6

    // }

Result is same key`s added value

1 Answers1

0

Check if the k is in the output, is so increment, else create

var output = {}
for (let e of input) {
    var k = e.keys()[0]
    if (k in output) {
        output[k] += e[k]
    } else {
        output[k] = e[k]
    }
}
TSR
  • 17,242
  • 27
  • 93
  • 197