What is the best way to convert JSON values into an array?
I am using an AJAX request to fetch the JSON data.
$.getJSON( "data/data.json", function( data ) {
var obj=data["DataContainer"]["profit"];
for(i in obj){
var temp=[];
temp.push(obj[i].current);
temp.push(obj[i].target);
data3.push(temp);
}
JSON.stringify(data3).replace(/\"/g, "");
console.log(JSON.stringify(data3).replace(/\"/g, ""));
});
The JSON data:
{
"DataContainer": {
"profit": [
{
"current": "60","target": "19"
},
{
"current": "55","target": "19"
},
{
"current": "55","target": "19"
},
{
"current": "55","target": "19"
},
{
"current": "55","target": "19"
},
{
"current": "55","target": "19"
}
]
}
}
The output of the console.log will be:
[[60,19],[55,19],[55,19],[55,19],[55,19],[55,19]]
Now I want to pass these values from console.log to a new variable:
var rows = [["Current profit","Target profit"]],
data3 = [[27, 19],[22,19],[35, 19],[23,19],[19, 19],[17,19]];
The expected results should be:
var rows = [["Current profit","Target profit"]],
data3 = [[60,19],[55,19],[55,19],[55,19],[55,19],[55,19]];
Any assistance on this matter would be greatly appreciated. Thank you.
To better comprehend the issue, I have shared my code on Jsfiddle (since I cannot load the JSON from GitHub)
Please refer to this JSFIDDLE