Question

I am having trouble parsing my csv file into an empty array. It seems that I can parse the file but I can't seem to fill the array.

Here is an abridged view my csv file (157 rows - showing 9 here):

"",percentage "1",0.275862068965517 "2",0.137931034482759 "3",0.133333333333333 "4",0.4 "5",0.633333333333333 "6",0.766666666666667 "7",0.379310344827586 "8",0.724137931034483 "9",0.933333333333333

Here is my code in d3:

var values = [];

d3.csv("wlythree.csv", function(data) {
  values = data.map(function(d) { return [ +d["percentage"] ]; });
});

console.log(values)

It prints out in the console an empty array. I put in the console.log just to check my code - it's not needed besides for that purpose. All I need is the percentage column in the array and the rest of my code works perfectly. I cannot seem to figure out and any help would be greatly appreciated!

Était-ce utile?

La solution

This should do the trick:

d3.csv("wlythree.csv", function(data) {
    var values = [];
    values = data.map(function(d) { return d.percentage; });

    console.log(values)
});

It yields:

["0.275862068965517", "0.137931034482759", ...]

EDIT: placed declaration of values inside the callback since the csv loading is asynchronous and that better reflects the script flow.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top