2017-10-17 7 views
0

d3のq.deferにパラメータを渡すにはどうすればよいですか? | q.deferにパラメータを渡す(d3.csv)

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? 
    }); 

がawaitAll内Dを取得する方法があります:ここで - は、私は、インデックス( Dファイル名)を渡す必要がありますおかげさまで 唯一の親スコープによる

答えて

1

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はマッパー関数内dにバインドされている)dataのすべてを使用している:

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()) 
    } 
}); 

awaitAllドキュメント:https://github.com/d3/d3-queue#queue_awaitAll

いくつかの追加の読み物:

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

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

+0

はい、しかし、私はd値に応じた結果が必要です。私が理解しているように、「結果」はスレッドであるか、間違っているかのように、常にランダムな順序になっていますか? – SERG

+1

'results'は、あなたが作成した全てのdefersの配列です。 'results [0]'は 'data [0]'に対応し、 'd == 1 'に対応します。 – cowbert

+0

ありがとう、私はそれが非同期であると思ったので、結果の順序はランダムになります。 – SERG

関連する問題