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.

4
  • Post whatever you have tried. Commented Jun 26, 2018 at 16:49
  • Your calculated output seems to differ. Commented Jun 26, 2018 at 16:58
  • i tried using reduce() function but it came out very complex to me coz i was not able to handle the non matching keys. Commented Jun 26, 2018 at 16:59
  • 1
    Possible duplicate of Javascript - Sum of two object with same properties Commented Jun 26, 2018 at 17:03

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

if(!arr[k]) arr[k] = 0; Will this line be setting the non matching key equal to zero, even if it has some value ?
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
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);

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.