I know this question has been asked before but their solutions dont see to work for me for some reason.
I am trying to populate two arrays with data from a CSV file where:
name,value
alpha,34
beta,12
delta,49
gamma,89
What I am trying now is
var field1=[];
var field2=[];
d3.csv("data.csv",function(csv){
csv.map(function(d){
field1.push(d.name);
field2.push(+d.value);
})
});
console.log("field1",field1);
console.log("field2",field2);
When I view console on my browser I see:
field1 Array [ ] field2 Array [ ]
where:
field1:
Array[0]
0:"alpha"
1:"beta"
2:"delta"
3:"gamma"
field2:
Array[0]
0:34
1:12
2:49
3:89
However when I try to access field1[0], I get the value as undefined
field1 undefined
What I guess is happening is field1 array has an array of arrays of "name" column, but I'm not able to access the first element though field[0][0] either. What I get is:
TypeError: field1[0] is undefined
I'm very new to JavaScript and I don't seem to understand why the array is not populated correctly as a 1 dimensional array or if I'm doing something wrong. I am aware that I can iterate through each row while accessing csv per row but I want to store the csv values in array to be used globally in the script.
Links I have gone through are:
- https://github.com/mbostock/d3/wiki/CSV
- converting a d3.csv method to d3.csv.parse method
- csv to array in d3.js
- http://learnjsdata.com/read_data.html
- http://bl.ocks.org/enjalot/1525346
But I seem to be missing or overlooking something.. Please help!