使用带有两个参数的函数的javascript映射 [英] Using javascript map with a function that has two arguments

查看:58
本文介绍了使用带有两个参数的函数的javascript映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

如何使用以下函数使用 map

How do I use map with the following function:

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

其中,我想为参数调整输入值当我调用map时手动,比如说 adjust = 2 ,并且值为 x 取自数组

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.

推荐答案

使用匿名函数:

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

这篇关于使用带有两个参数的函数的javascript映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆