Reputation: 3668
Please help me in convert in the below strings to actual data.
[ { "key": "model1", "values": [ "[1001874600000,16]", "[1001961000000,11]", "[1002047400000,14]", "[1002133800000,19]" ] }, { "key": "model2", "values": [ "[1001874600000,14]", "[1001961000000,18]", "[1002047400000,14]", "[1002133800000,12]" ] }, { "key": "model3", "values": [ "[1001874600000,14]", "[1001961000000,13]", "[1002047400000,11]", "[1002133800000,20]" ] }, { "key": "model4", "values": [ "[1001874600000,11]", "[1001961000000,11]", "[1002047400000,17]", "[1002133800000,11]" ] } ]
I used the below function to convert a flat data to the above format.
d3.csv("data.csv", function(data) { data.forEach(function(d){ d.count = +d.count; d.date = Date.parse(d.date); }); var nest = d3.nest() .key(function(d) {return d.model;}) .rollup(function(v) {return v.map(function(d) {return "[" + d.date + "," + d.count + "]";})}) .entries(data) d3.select('body').append('pre') .text(JSON.stringify(nest, null, ' ')); });
Now i just need to remove the double quotes in the values field. Some one please help me in this, I don't have any clue on how to do it.
Help will be much pleased.
Thanks in advance.
Upvotes: 0
Views: 2655
Reputation: 3737
Not exactly on topic, but there is something that anyone reading this should be aware of. You should stay as far away from the eval() function unless you 100%, and I can't stress the 100% enough, know the incoming data (i.e. not coming from another source and controlled by you).
Here is some reading to help provide more details (what are the issues javascript eval can pose).
Upvotes: 0
Reputation: 29416
I know, eval
is evil, but you can use it to convert your strings to lists like so:
var data = '[1,2,3]';
data = eval(data); // returns [1,2,3]
Or you can rewrite the following return
statement
return "[" + d.date + "," + d.count + "]";
like so
return [d.date, d.count];
Upvotes: 0