Reputation: 1118
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.
Upvotes: 0
Views: 67
Reputation: 151
try this
var graphData = [];
for (let obj of one[0].dps) {
graphData.push(obj);
}
for (let obj of two[0].dps){
graphData.push(obj);
}
Upvotes: 0
Reputation: 1361
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]
...
Upvotes: 0
Reputation: 1197
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);
Upvotes: 0
Reputation: 68393
Use map
var output = Object.keys(one[0].dps).map( s => [ s, one[0].dps[s], two[0].dps[s] ] )
Upvotes: 3