QuepasaTio QuepasaTio - 3 months ago 14
Javascript Question

Javascript Converting Data syntax - Removing Quotes

I am converting some data for some charts that I'm using.

The data that the charts can read need to look like this:

[[1423008000000,1],[1423094400000,1],[1425513600000,42]]


The data that I have to convert looks like this:

{"1300752000000":11,"1301356800000":4,"1329782400000":22}


I then use this code to convert it:

$.getJSON('myurl', function(mydata) {

var data = mydata;
var res = Object.keys(data).map(itm => [itm, data[itm]]);
var converted_data = res;


converted_data then gives my this:

[["1300752000000", 11], ["1301356800000", 4], ["1329782400000", 22]]


The converted data is adding " " - Quotes .... I need it to look like this:

[[1300752000000, 11], [1301356800000, 4], [1329782400000, 22]]


How can I get this converting code to convert without the quotes?

Answer

Object key's are always strings. You therefore need to cast itm to a number. Either by;

var res = Object.keys(data).map(itm => [+itm, data[itm]]);

... or more explicitly;

var res = Object.keys(data).map(itm => [Number(itm), data[itm]]);