如何将多个参数传递到下划线 JS 链中?

How do I pass multiple parameters into an underscore JS chain?

我已经尝试阅读文档,我可能遗漏了一些东西,但基本上我只是想将一个额外的参数传递给 underscoreJS 链方法以在映射过程中使用:

function(list, flag) {
  return _.chain(list)
          .filter(firstMethod)
          .map(secondMethod, flag) // I want to pass in the flag to this function
          .value();
}

这有意义吗?我猜我可能需要结合使用另一种下划线方法,但我不确定是哪个!

您应该使用 bind 来实现。它将创建一个绑定到您选择的实例(第一个参数)的新函数,它将使用实例一之后给出的参数调用:

function(list, flag) {
  return _.chain(list)
    .filter(firstMethod)
    .map(secondMethod.bind(this, flag))
    .value();
}