小编mkw*_*wng的帖子

如何确保D3在javascript运行之前完成加载多个CSV?

我正在使用D3 将其他 CSV文件列表的CSV 加载到javascript中.

当我运行以下代码时,employees数组在代码中到达它时仍然是空的.有没有一种正确的方法来确保D3在javascript继续之前完成加载数据?

var employees = [];

//Retrieve the file list of all the csvs in the data directory, then run a callback on them
function retrieveList(url,callback) {
    d3.csv(url,function(data) {
        callback(data);
    })
}

//Parse a file list, and then update the employee array with the data within
function parseList(filenames){
    filenames.forEach(function(d) {
        d3.csv(d.filename,function(data) {
            data.forEach(function(d) employees.push(d.name));
        }
    }
}

//Run this code
var filenamesUrl = "http://.../filenames.csv"
retrieveList(filenamesUrl, parseList);

console.log(employees); //This logs "[]"
Run Code Online (Sandbox Code Playgroud)

如果我在Chrome中加载页面,当我进入控制台并记录员工时,肯定会返回充满名称的数组.当我在最后一行运行console.log(employees)时,我该怎么做?

javascript csv d3.js

14
推荐指数
2
解决办法
6538
查看次数

标签 统计

csv ×1

d3.js ×1

javascript ×1