Andrei Răileanu Andrei Răileanu - 4 months ago 16
Javascript Question

Manipulate JSON data into another object

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

Answer

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