使用带有两个参数的函数的 javascript 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12344087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:46:01  来源:igfitidea点击:

Using javascript map with a function that has two arguments

javascript

提问by Curious2learn

I know that I can use mapwith a function of one variable in the following manner:

我知道我可以通过map以下方式使用一个变量的函数:

var squarefunc = function(x) {
    return x*x;
};
values = [1,2,3,4]
values.map(squarefunc) // returns [1,4,9,16]

How do I use mapwith the following function:

我如何使用map以下功能:

var squarefuncwithadjustment = function(x, adjustment) {
    return (x*x + adjustment);
}

where, I want to input value for argument adjustmentmanually when I call map, say adjustment=2, and have the value of xtaken from the array values.

其中,我想adjustment在调用 map 时手动输入参数值,例如adjustment=2,并x从数组中获取值values

回答by casablanca

Use an anonymous function:

使用匿名函数:

values.map(
  function(x) { return squarefuncwithadjustment(x, 2); }
);

回答by Peter

You could use a callback creation function:

您可以使用回调创建函数:

var createSquareFuncWithAdjustment = function(adjustment) {
    return function(x) { return (x * x) + adjustment; };
};

values = [1, 2, 3, 4];
values.map(createSquareFuncWithAdjustment(2)); // returns [3, 6, 11, 18]

回答by Dima Vinogradov

As of ES6 you can use:

从 ES6 开始,您可以使用:

.map((element) => func(element,params...))

In your case if I want to use 3 as adjustment:

在您的情况下,如果我想使用 3 作为调整:

values = [1,2,3,4]
values.map(n => squarefuncwithadjustment(n,3))

回答by gray state is coming

If you reverse the order of your arguments, you can bind the adjustment as the first argument, so that the xwill be passed as the second.

如果您颠倒参数的顺序,您可以将调整绑定为第一个参数,以便x将作为第二个参数传递。

var squarefuncwithadjustment = function(adjustment, x) {
    return (x*x + adjustment);
}

values.map(squarefuncwithadjustment.bind(null, 2)); // [3, 6, 11, 18]

The first argument to .bindsets the calling context, which doesn't matter here, so I used null. The second argument to .bindbinds 2as the first argument when invoked.

.bind设置调用上下文的第一个参数,在这里无关紧要,所以我使用了null. 调用时,第二个参数.bind绑定2为第一个参数。

It may be better to store the function as a bound version.

将该函数存储为绑定版本可能会更好。

var squareFuncWith2 = squarefuncwithadjustment.bind(null, 2);

Then use it with .map.

然后与.map.

values.map(squareFuncWith2); // [3, 6, 11, 18]

回答by ajaykumar

Well!! You can easily pass a second parameter to the map function. The following method is widely used to pass this parameter which generally gets hidden during the call:

好!!您可以轻松地将第二个参数传递给 map 函数。以下方法广泛用于传递此参数,该参数通常在调用期间隐藏:

values.map(function(x , this) {
    return x*x + this.adjustment;
});

var adjustment = 1;
var values = [1,2,3,4]
values.map(function(x , adjustment) {
    return x*x + adjustment;
});

OR

或者

var adjustment = 1;
var squarefunc = function(x , adjustment) {
    return x*x + adjustment;
};
values = [1,2,3,4]
values.map(squarefunc);

回答by leerssej

To perform this within a single function, you can add a dash of IIFE to your Curry.

要在单个函数中执行此操作,您可以在 Curry 中添加少量 IIFE。

function mapSingleFunc(values, adjustment) {
  return values.map((adjustment => x => (x * x) + adjustment)(adjustment));
};
console.log(mapSingleFunc([1,2,3,4], 2))

In the most abstract sense you are able to tunnel your valuesin through the calling array. Adding an IIFE allows you to feed the adjustmentin each time in the closure.

在最抽象的意义上,您可以values通过调用数组进行隧道输入。添加一个 IIFE 允许您adjustment在每次关闭时输入。

回答by MCFreddie777

ES6+ :

ES6+:

values.map( x => squarefuncwithadjustment(x,2) );

回答by Menzies

var squarefuncwithadjustment = (x, adjustment) => { return (x*x + adjustment); }

var squarefuncwithadjustment = (x, adjustment) => { return (x*x + adjustment); }

Then

然后

values = values.map( x => squarefuncwithadjustment(x, 2) );

values = values.map( x => squarefuncwithadjustment(x, 2) );