1

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

DiggiDan
  • 13
  • 4
  • Looks really invalid, both as JSON and as javascript objects ? – adeneo Jan 10 '15 at 20:48
  • possible duplicate of [Converting JSON Object into Javascript array](http://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array) – Tyr Jan 10 '15 at 21:04

1 Answers1

2

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.

Overv
  • 8,433
  • 2
  • 40
  • 70
  • When i try to parse it with this code i get this: `[[NaN, 25.4], [NaN, 25.3], [NaN, 28.7], [NaN, 33.5], [NaN, 35.6]]` I have edited my question if that helps. – DiggiDan Jan 10 '15 at 21:09
  • Yep, it seems that i cant convert the string in the format "2015-01-04 21:05:16". It works if i manually put in "2015-01-04" in the Date() function though so i dont know if theres a problem with the space. – DiggiDan Jan 10 '15 at 22:02
  • @DiggiDan Which browser are you using? – Overv Jan 10 '15 at 22:31
  • @DiggiDan See the new code, tested in Firefox and Chrome. – Overv Jan 10 '15 at 22:37