[英]Change a javascript function based on input in html
dataset = data.map(function(d)
{ return [ +d["Control 1"], +d["Control 2"],
+d["Exp3"], +d["Exp4"], +d["Exp5"], +d["Exp6"] ];
});
I want to be able to insert other +d's into the function based on user file input (I already have it in an array like ["exp7","exp8"...]). 我希望能够根据用户文件输入将其他+ d插入到函数中(我已经在[[exp7“,” exp8“ ...]这样的数组中添加了它)。 How do I write it so that the array is like +d ["exp7"] and so on and so forth?
我如何编写它,使数组像+ d [“ exp7”]之类,依此类推?
I'm writing this in html. 我用html编写。
Thanks! 谢谢!
I think this would be a good place to make use of currying. 我认为这将是使用Curry的好地方。
You could make a helper that does something like this: 您可以使一个助手执行以下操作:
function t(fields) {
return function (data) {
return fields.map(function (field) {
return +data[field]
})
}
}
And then you would make use of it like: 然后您将使用它,例如:
dataset = data.map(t([
"Control 1",
"Control 2",
"Exp3",
"Exp4",
"Exp5",
"Exp6"
]));
This way you can pass in your other arrays of keys in directly. 这样,您可以直接传入其他键数组。
edit: If your operating in an ES6 environment you could use the simple one-line combinator: 编辑:如果您在ES6环境中操作,则可以使用简单的单行组合器:
const t = fs => d => fs.map(f => +d[f])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.