0

My input in one single json

input = [{

        "201609": 0,
        "201610": 0,
        "201611": 0,
        "201804": 130,
        "201805": 130,
        "fy16Q3": 17,
        "fy17Q1": 0,
        "fy17": 0,
        "fy17Q2": 0,
        "fy17Q3": 0

}, {

        "201510": 0,
        "201610": 0,
        "201611": 10,
        "201803": 20,
        "201804": 30,
        "201805": 40,
        "201806": 130,
        "201809": 130,
        "fy17Q1": 2,
        "fy17": 3,
        "fy17Q2": 5,
        "fy17Q3": 6
}];

In the output i want to iterate through all the elements of this json and sum the values of the matching keys. Also keep the non matching lone keys in the output as well.

output =
[{

        "201510": 5,         // no matching pair
        "201609": 3,         // no matching pair
        "201610": 6+9 = 15,  // matching pair exist
        "201611": 10+12 = 22,
        "201803": 20,
        "201804": 30+13 = 33,
        "201805": 40+14 = 44,
        "201806": 130,
        "201809": 130,
        "fy16Q3": 17,           // no matching pair
        "fy17Q1": 2+7 = 9,      // matching pair exist
        "fy17": 3+8 = 11,
        "fy17Q2": 5+9 = 14,
        "fy17Q3": 6+100 = 106
}];

The problem is that iam not able to figure out how to handle the keys which don't have a matching pair.

user3205921
  • 51
  • 1
  • 8

4 Answers4

2

You can try the following code. Your desired output looks different than your logic

var data = input = [{

        "201609": 0,
        "201610": 0,
        "201611": 0,
        "201804": 130,
        "201805": 130,
        "fy16Q3": 17,
        "fy17Q1": 0,
        "fy17": 0,
        "fy17Q2": 0,
        "fy17Q3": 0

}, {

        "201510": 0,
        "201610": 0,
        "201611": 10,
        "201803": 20,
        "201804": 30,
        "201805": 40,
        "201806": 130,
        "201809": 130,
        "fy17Q1": 2,
        "fy17": 3,
        "fy17Q2": 5,
        "fy17Q3": 6
}];
var output = data.reduce((arr,d,x) =>{
  var keys = Object.keys(d);
  keys.forEach( (k) => {
    if(!arr[k]) arr[k] = 0;
    arr[k] = arr[k] + d[k];
  })
  return arr;
},{});

console.log(output);
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
  • if(!arr[k]) arr[k] = 0; Will this line be setting the non matching key equal to zero, even if it has some value ? – user3205921 Jun 26 '18 at 18:23
  • This will append the key if it already doesn't exist in the output object with dummy value 0. Then it will update with the actual value in the input array – Md Johirul Islam Jun 26 '18 at 18:39
1

input = [{

        "201609": 0,
        "201610": 0,
        "201611": 0,
        "201804": 130,
        "201805": 130,
        "fy16Q3": 17,
        "fy17Q1": 0,
        "fy17": 0,
        "fy17Q2": 0,
        "fy17Q3": 0

}, {

        "201510": 0,
        "201610": 0,
        "201611": 10,
        "201803": 20,
        "201804": 30,
        "201805": 40,
        "201806": 130,
        "201809": 130,
        "fy17Q1": 2,
        "fy17": 3,
        "fy17Q2": 5,
        "fy17Q3": 6
}];

var output = input.reduce((p,c) => {
 for(let k in c){
     p[k] = (p[k] || 0) + c[k]; 
 }
 return p;
},{});

console.log(output);
0

input = [{

        "201609": 0,
        "201610": 0,
        "201611": 0,
        "201804": 130,
        "201805": 130,
        "fy16Q3": 17,
        "fy17Q1": 0,
        "fy17": 0,
        "fy17Q2": 0,
        "fy17Q3": 0

}, {

        "201510": 0,
        "201610": 0,
        "201611": 10,
        "201803": 20,
        "201804": 30,
        "201805": 40,
        "201806": 130,
        "201809": 130,
        "fy17Q1": 2,
        "fy17": 3,
        "fy17Q2": 5,
        "fy17Q3": 6
}];
var output = [{}];


for( i in input){    
  for (key in input[i]){
    if(output[0].hasOwnProperty(key)){
    output[0][key]+=input[i][key];
    }else{
    output[0][key]=input[i][key];
    }
}

}

console.log(output)
Saif
  • 2,530
  • 3
  • 27
  • 45
0

Use array reduce method. In this method take the first object of the input array as the initial object since.SInce object keys are always unique, for any matching key just update the value

var input = [{

  "201609": 0,
  "201610": 0,
  "201611": 0,
  "201804": 130,
  "201805": 130,
  "fy16Q3": 17,
  "fy17Q1": 0,
  "fy17": 0,
  "fy17Q2": 0,
  "fy17Q3": 3

}, {

  "201510": 0,
  "201610": 0,
  "201611": 10,
  "201803": 20,
  "201804": 30,
  "201805": 40,
  "201806": 130,
  "201809": 130,
  "fy17Q1": 2,
  "fy17": 3,
  "fy17Q2": 5,
  "fy17Q3": 6
}];
// the array will start reducing from second element that is 
// element from index 1
let toLoopArray = input.slice(1, input.length);
let output = input.reduce(function(acc, curr) {
  // for the current object check if the key already exist
  // if not then create the new key and update value
  for (let keys in curr) {
    if (!acc.hasOwnProperty(keys)) {
      acc[keys] = curr[keys]
    } else {
      // acc[keys] = acc[keys] + curr[keys]
      console.log(curr[keys])
      acc[keys] = acc[keys] + curr[keys]
    }

  }

  return acc;
}, input[0])
console.log([output])
brk
  • 48,835
  • 10
  • 56
  • 78