如何将参数传递给 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/