0

I am working currently on javascript project. In my project i have two json array like:

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

[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.

2
  • JSON is a string not an object. If you want to make a JSON String to an object you have to call JSON.parse(myJSONstring) Commented Jan 23, 2018 at 12:35
  • You don't need the inner loop Commented Jan 23, 2018 at 12:37

4 Answers 4

3

Use map

var output = Object.keys(one[0].dps).map( s => [ s, one[0].dps[s], two[0].dps[s] ] )
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

try this

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

1 Comment

Consider adding an explanation of what your code does, what the output looks like, why is it better than the OP's code, etc.

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.