使用带有两个参数的函数的 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
Using javascript map with a function that has two arguments
提问by Curious2learn
I know that I can use map
with 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 map
with the following function:
我如何使用map
以下功能:
var squarefuncwithadjustment = function(x, adjustment) {
return (x*x + adjustment);
}
where, I want to input value for argument adjustment
manually when I call map, say adjustment=2
, and have the value of x
taken 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 x
will 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 .bind
sets the calling context, which doesn't matter here, so I used null
. The second argument to .bind
binds 2
as 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 values
in through the calling array. Adding an IIFE allows you to feed the adjustment
in 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)
);