将名称传递给JavaScript中for循环内的函数?

时间:2019-04-10 11:06:51

标签: javascript function for-loop variables

我在使用某些JavaScript时遇到问题。我正在尝试制作复合dc-js图表。当我尝试传递变量名时,它会消失在函数中。

var all_functions_array = []; //

banks = ['BANK1','BANK2','BANK3']

for (var i = banks.length - 1; i >= 0; i--) {
    var bank = banks[i]
    all_functions_array[i] = dateDim.group().reduceSum(function(d) {return d[bank]})
}
// Trying to pass [bank] here in the return but it dosnt work.

我该如何解决?

这是我的数据,csv:

"date","BANK1","BANK2","BANK3","BANK4","BANK5"
1483115384,123,13.7,15,4,6
1483115384,123,13.7,898,5,6
1483114915,132,13.7,415,4,7
1483112683,132,13.8,73,3,7

这就是制作dateDim的方法:

var url = "tickdata.csv";

d3.csv(csv).then(function(data) {

var ndx = crossfilter(data);
var all = ndx.groupAll();

var dateDim = ndx.dimension(function (d) { return d.date; });

0 个答案:

没有答案