根据html中的输入更改javascript函数

时间:2016-12-15 04:49:02

标签: javascript html plotly

dataset = data.map(function(d) 
              { return [ +d["Control 1"], +d["Control 2"],
                +d["Exp3"], +d["Exp4"], +d["Exp5"], +d["Exp6"] ]; 
              });

我希望能够根据用户文件输入将其他+ d插入到函数中(我已经将它放在像[“exp7”,“exp8”...]这样的数组中)。 如何编写它以使数组类似于+ d [“exp7”]等等?

我是用html写的。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这是一个利用currying的好地方。

你可以做一个像这样的事情的帮手:

function t(fields) {
  return function (data) {
    return fields.map(function (field) {
      return +data[field]
    })
  }
}

然后你会像以下一样使用它:

dataset = data.map(t([
  "Control 1",
  "Control 2",
  "Exp3",
  "Exp4",
  "Exp5",
  "Exp6"
]));

这样您就可以直接传入其他数组键。

编辑: 如果您在ES6环境中操作,您可以使用简单的单行组合器:

const t = fs => d => fs.map(f => +d[f])