DiggiDan
DiggiDan

Reputation: 13

Remake json from object to array?

I'm so lost in space right now. I've sat for like 2-3h to find a solution to this problem but i just keep failing. Maybe i've gone blind or something. I hate asking for help but i really need it right now.

I have a json-response from a page wich is in this format:

[Object { timestamp="2015-01-04 21:05:16", value="25.4"},
Object { timestamp="2015-01-04 21:10:27", value="25.3"},
Object { timestamp="2015-01-04 21:15:38", value="28.7"},
Object { timestamp="2015-01-04 21:20:49", value="33.5"}]

And i need for it to look like this:

[ [1183939200000,40.71],
[1184025600000,40.38],
[1184112000000,40.82],
[1184198400000,41.55],
[1184284800000,41.18],
[1184544000000,41.06]]

I have tried soo much stuff and i feel so stupid asking for this because its probably the easiest thing in the world.

Thank you in advance!

Edit:

Apparently what Im getting back from the call is:

[{"timestamp":"2015-01-04 21:05:16","value":"26.9"},
{"timestamp":"2015-01-04 21:10:27","value":"27.1"},
{"timestamp":"2015-01-04 21:15:38","value":"24.8"},
{"timestamp":"2015-01-04 21:20:49","value":"21.4"},
{"timestamp":"2015-01-04 21:26:01","value":"19.6"}]

I got the "object" one when i console.debug:ed it. Dont know if this makes any difference or not.

Thanks

Upvotes: 1

Views: 209

Answers (1)

Overv
Overv

Reputation: 8519

Assuming that the second value in the array should be equal to the value member of the object:

arr = arr.map(function(obj) {
    return [
        Date.parse(obj.timestamp.replace(' ', 'T')),
        +obj.value
    ];
});

The Date.parse function converts a string like "2015-01-04T21:05:16" to a JavaScript timestamp (ISO 8601 format). That's why your current format needs to be changed slightly.

Upvotes: 2

Related Questions