Reputation: 1
const disorders=[];
const arr = [1,2,3,4,5];
d3.csv("https://gist.githubusercontent.com/victoirebeaufils/bdc33d6da0fb98eacf4b6c7d630c280b/raw", function(data){
const disorder = data.disorder;
disorders.push(data.disorder);
});
console.log(disorders)
console.log(disorders.length)
console.log(arr)
My array shows a length of 5 in the console when I expand the [] tab, but disorder.length
returns 0 and I can't access any elements.
The first array works well and also shows the number of elements as well as the elements in the array without needing to expand the tab. I'm not sure what that means or how to fix it so that the second one works properly.
Upvotes: 0
Views: 252
Reputation: 3629
Since the d3.csv() works async, you need to have a callback function for that:
for eg:
const disorders=[];
const arr = [1,2,3,4,5];
function execute(callback) {
d3.csv("https://gist.githubusercontent.com/victoirebeaufils/bdc33d6da0fb98eacf4b6c7d630c280b/raw", function(data){
const disorder = data.disorder;
disorders.push(data.disorder);
callback(disorders);
});
}
execute(function(result) {
console.log(result)
console.log(result.length)
console.log(arr)
});
Upvotes: 1