The best way to combine two objects and sum the values โ€‹โ€‹of the same key?

I have two objects like this:

let data = [[
  {
    10: {key1: 1, key2: 2, key3: 3},
    11: {key1: 1, key2: 2, key3: 3},
    12: {key1: 1, key2: 2, key3: 3},
    },
  {},
  {}
],
[
  {
    10: {key1: 1, key2: 2, key3: 3},
    11: {key1: 1, key2: 2, key3: 3},
    12: {key1: 1, key2: 2, key3: 3},
    },
  {},
  {}
]]

      

I want to create a new object and sum each value of each key. Like this:

let newData = [
      {
        10: {key1: 2, key2: 4, key3: 6},
        11: {key1: 2, key2: 4, key3: 6},
        12: {key1: 2, key2: 4, key3: 6},
        },
      {},
      {}
    ]

      

Each object has three objects in it. These three objects have 45 keys, each with an object with three keys / values.

I cannot find any good solution for summing each value.

My current solution is that I first loop through data

:

let count = 0;
let count2 = 0;
for(let ob of data){
     for(let child in ob[0]){
          keyOne = ob[0][child].keyOne;
          keyTwo = ob[0][child].keyTwo;
          keyThree = ob[0][child].keyThree;
          keyFour = ob[0][child].keyFour;
          if(count < 45) {
               newData[0][count].keyOne += keyOne;
               newData[0][count].keyTwo += gkeyTwop;
               newData[0][count].keyThree += keyThree;
               newData[0][count].keyFour += keyFour;
          } else {
               newData[0][count2].keyOne += keyOne;
               newData[0][count2].keyTwo += keyTwo;
               newData[0][count2].keyThree += keyThree;
               newData[0][count2].keyFour += keyFour;
               count2++;

          }
          count++;
     }

      

In the worst case, the data was received by three objects with 45 keys each. Then I have to do it three times. This seems very bad. There must be a better way. Tips?

+3


source to share


2 answers


You can use Array#reduce

and match the values โ€‹โ€‹of the internal array after adding the values.



var data = [[{ 10: { key1: 1, key2: 2, key3: 3 }, 11: { key1: 1, key2: 2, key3: 3 }, 12: { key1: 1, key2: 2, key3: 3 }, }, {}, {}], [{ 10: { key1: 1, key2: 2, key3: 3 }, 11: { key1: 1, key2: 2, key3: 3 }, 12: { key1: 1, key2: 2, key3: 3 }, }, {}, {}]],
    result = data.reduce((a, b) => 
        a.map((v, i) =>
            (Object.keys(b[i]).forEach(k =>
                Object.keys(b[i][k]).forEach(l => 
                    v[k][l] = (v[k][l] || 0) + b[i][k][l]))
            , v)
        )
    );

console.log(result);
      

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

Run codeHide result


+1


source


Here is a solution with reduce()

and multiple loops forEach()

because you also need to nest nested objects as well.



var data = [[{"10":{"key1":1,"key2":2,"key3":3},"11":{"key1":1,"key2":2,"key3":3},"12":{"key1":1,"key2":2,"key3":3}},{},{}],[{"10":{"key1":1,"key2":2,"key3":3},"11":{"key1":1,"key2":2,"key3":3},"12":{"key1":1,"key2":2,"key3":3}},{},{}]]

var result = data.reduce(function(r, e, i) {
  if (i == 0) r = r.concat(e)
  else  {
  e.forEach(function(a, j) {
       Object.keys(a).forEach(function(key) {
          if (!r[j][key]) r[j][key] = a[key]
          else {
            Object.keys(a[key]).forEach(function(k) {
              r[j][key][k] += a[key][k]
            })
          }
      })
    })
  }
  return r;
}, [])

console.log(result)
      

Run codeHide result


+1


source







All Articles