Javascript Json:通过匹配JSON键求和

时间:2018-06-26 16:47:27

标签: javascript arrays json node.js

我在一个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
}];

在输出中,我想遍历此json的所有元素并求和匹配键的值。还要在输出中保留不匹配的单独键。

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
}];

问题是我无法弄清楚如何处理没有匹配对的钥匙。

4 个答案:

答案 0 :(得分:1)

您可以尝试以下代码。您想要的输出看起来与您的逻辑不同

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

答案 1 :(得分: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)

答案 2 :(得分:0)

使用数组缩减方法。在此方法中,将input数组的第一个对象作为初始对象。因为对象键始终是唯一的,对于任何匹配的键,只需更新值即可。

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

答案 3 :(得分: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 = input.reduce((p,c) => {
	for(let k in c){
    	p[k] = (p[k] || 0) + c[k]; 
	}
	return p;
},{});

console.log(output);