【发布时间】:2015-10-01 08:56:06
【问题描述】:
我是 D3 和 JS 的新手,并尝试了解如何在 D3 中的数据对象上调用具有多个参数的函数。在我的尝试之下。问题是如何使第 2 版正常工作?
d3.csv(input_file, function(data) {
var metric = "foo";
// Version 1:
// clean number format. works fine
data.forEach(function(d) {
d[metric] = +d[metric].replace(/,/g,''); //remove thousand separator
return d;
});
//Version 2:
// this doesn't work. why not?
// [Error] TypeError: undefined is not a function (evaluating 'd[metric].replace(/,/g,'')')
data.forEach(function(d) {
return cleanNumberFormat(d, metric);
});
// ...
});
// define a function to be used in several places
function cleanNumberFormat(d, metric) {
d[metric] = +d[metric].replace(/,/g,''); //remove thousand separator
return d;
}
【问题讨论】:
-
看起来应该可以了。您能否提供一个重现问题的完整示例?
标签: javascript d3.js parameter-passing