Reputation: 137
I am trying to figure out how to manipulate my JSON data and create a separated object with the manipulated data.
To be more precise, I am using this JSON file: https://jsonblob.com/57a70ca4e4b0dc55a4eb1f73
I am trying to convert [dataset][data] into this format:
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549]
So I need to use the fields [dataset][data][i][0]
(Date) and [dataset][data][i][4]
(Value) and somehow put it in a new JSON formatted object but I have no idea how to do it.
Someone can help me with this situation?
$(function () {
$.getJSON('json/AAPL.json', function (data) {
// Data manipulation into a new object??
});
});
Upvotes: 1
Views: 116
Reputation: 4612
Your problem is just map one :
You have an array of elements (arrays themselves) and you want to keep and modify just first and fifth element of each sub-array
So you can do it like this
var data = [
[
"2016-08-02",
106.05,
106.07,
104,
104.48,
32731069,
0,
1,
106.05,
106.07,
104,
104.48,
32731069
],
[
"2016-08-01",
104.41,
106.15,
104.41,
106.05,
37141424,
0,
1,
104.41,
106.15,
104.41,
106.05,
37141424
],
[
"2016-07-29",
104.19,
104.55,
103.68,
104.19,
27076805,
0,
1,
104.19,
104.55,
103.68,
104.19,
27076805
]
];
var result = data.map(x => [Date(x[0]), x[4]]);
console.log(result); // [ [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.48 ],
// [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',106.05 ],
// [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.19 ] ]
Upvotes: 1
Reputation: 3130
This should do it:
$(function () {
$.getJSON('json/AAPL.json', function (data) {
var correctFormat = data.dataset.data.map(function(item) {
return [new Date(item[0]), item[4]];
});
});
});
Upvotes: 1