javascript - 将参数传递给 q.defer (d3.csv)

标签 javascript csv d3.js

如何将参数传递给 d3 中的 q.defer ? 我需要在此处传递索引(文件名 - d ):

    var data = [1,3,5,6,7];
        var q = d3.queue();
        data.map(function(d){
            q.defer(d3.csv,'https://***/'+d+'.csv', d); //something like pass d
        })
        q.awaitAll(function(error, result, d) {//and get the d here according to results
                      if (error) throw error;
//how to get the d value here?
    });

有没有办法让 d 进入awaitAll| 谢谢。

最佳答案

仅通过父作用域:

var myd = data.map(function(d){
    q.defer(d3.csv,'https://***/'+d+'.csv', d); 
    return d
})

q.awaitAll(function(error, result, d) {
    if (error) throw error;
    //how to get the d value here?
    console.log(myd) // myd contains all the d from your map.
});

但是由于在 data.map() 调用中,您已经使用了所有 data (data 绑定(bind)到 d 映射器函数内):

q.awaitAll(function(error, result, d) {
    if (error) throw error;
    //how to get the d value here?
    console.log(data) // this is the same `data` from the `data.map()`
});

编辑:原来的问题不清楚。 OP 显然想要将结果重新映射回 d 值:

q.awaitAll(function(error, result) {
    if (error) throw error;
    //how to get the d value here?
    result.map(function(res, idx) {
        console.log("original d:" + data[idx].toString())
    }
});

等待所有文档:https://github.com/d3/d3-queue#queue_awaitAll

一些额外的阅读 Material :

https://scotch.io/tutorials/understanding-scope-in-javascript

https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/

关于javascript - 将参数传递给 q.defer (d3.csv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46796350/

相关文章:

css - 在 d3 js 图表中扩展轴条高度

javascript - 如何根据条件更改折线图中数据点的颜色?

javascript - 如果 element 未定义,document.getelementbyId 将返回 null?

javascript - 使用 fileReader 读取多张图像

java - JsonGenerationException : CSV generator does not support Object values for properties

python - 无法在python中将xml文件转换为csv

java - 使用 Java 将分段函数转换为 CSV 文件

javascript - HTML5 文件上传 block /切片导致 CPU 使用率高

Javascript 数组未加入 | (或)字符

javascript - NS_ERROR_DOM_BAD_URI : Access to restricted URI denied - Getting in in trying to use a D3. csv