简体   繁体   English

合并多个json对象

[英]Merge multiple json objects

I am working currently on javascript project. 我目前正在从事javascript项目。 In my project i have two json array like: 在我的项目中,我有两个json数组,例如:

var one = [{
"dps": {
        "1516624200": 0.03333333333333333,
        "1516624800": 0.88,
        "1516625400": 0.0,
        "1516626000": 0.0,
        "1516626600": 0.0,
        "1516627200": -0.012962962962963048,
        "1516627800": -0.022390572390572364,
        "1516628400": 20.37605723905724,
        "1516629000": 80.06746296296296,
        "1516629600": 94.12977777777778,
        "1516630200": 94.11733333333332,
        "1516630800": 94.1302777777778,
        "1516631400": 94.19999999999995
        }
}]  

var two = [{
"dps": {
        "1516624200": 0.2,
        "1516624800": 0.95,
        "1516625400": 0.0,
        "1516626000": 0.0,
        "1516626600": 0.0,
        "1516627200": -0.55,
        "1516627800": -0.6,
        "1516628400": 20.77,
        "1516629000": 8296296296,
        "1516629600": 99,
        "1516630200": 99,
        "1516630800": 55,
        "1516631400": 94.19999999999995
        }
}]

I want to merge these two array and finaloutput should be like these type of array 我想合并这两个数组,并且finaloutput应该像这些类型的数组

[1516624200,0.03333333333333333,0.2],[1516624800,0.88,0.0]  

I am using like: 我正在使用像:

 var graphData = [];
    var getValue = data[0].dps;
    var getVal = data[1].dps;
    for(var i in getVal){
        for(j in getVal){
            graphData.push([new Date(i*1000),getVal[i],getVal[i]])
        }
    }

This give me wrong values. 这给了我错误的价值观。 Please help. 请帮忙。

使用map

var output = Object.keys(one[0].dps).map( s => [ s, one[0].dps[s], two[0].dps[s] ] )

try this

var graphData = [];
for (let obj of one[0].dps) {
graphData.push(obj);
}
for (let obj of two[0].dps){
graphData.push(obj);
}

This should do it: 应该这样做:

var graphData = [];
var newGraphDataValue = [];
var oneDps = one[0].dps;
var twoDps = two[0].dps;

Object.keys(oneDps).forEach(function(oneDpsKey){
    newGraphDataValue = [oneDpsKey, oneDps[oneDpsKey]];
    Object.keys(twoDps).forEach(function(twoDpsKey){
    if (twoDpsKey === oneDpsKey) {
        newGraphDataValue.push(twoDps[twoDpsKey]);
    }
  });
  graphData.push(newGraphDataValue);
});

console.log('Result: ', graphData);

Minor modification to your code: 对您的代码进行少量修改:

  var graphData = [], i, j, d;

  var getValue = one[0].dps;
  var getVal = two[0].dps;
  for(i in getValue){
    for(j in getVal){
      d = new Date(i*1000);
      graphData.push([d.getTime(),getValue[i], getVal[j]]);
    }
  }
  console.log(graphData);

Got the results like the following: 得到如下结果:

(3) [1516624200000, 0.03333333333333333, 0.2]
(3) [1516624200000, 0.03333333333333333, 0.95]
(3) [1516624200000, 0.03333333333333333, 0]
(3) [1516624200000, 0.03333333333333333, 0]
(3) [1516624200000, 0.03333333333333333, 0]
(3) [1516624200000, 0.03333333333333333, -0.55]
(3) [1516624200000, 0.03333333333333333, -0.6]
(3) [1516624200000, 0.03333333333333333, 20.77]
(3) [1516624200000, 0.03333333333333333, 8296296296]
(3) [1516624200000, 0.03333333333333333, 99]
(3) [1516624200000, 0.03333333333333333, 99]
(3) [1516624200000, 0.03333333333333333, 55]
(3) [1516624200000, 0.03333333333333333, 94.19999999999995]
(3) [1516624800000, 0.88, 0.2]
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM