javascript 将数组中的所有值作为参数传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19570629/
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-10-27 16:00:51  来源:igfitidea点击:

Pass all the values from an array into a function as parameters

javascriptarraysfunctionparameter-passing

提问by Randy Hall

I have an array of values:

我有一个值数组:

['a', 'b', 'c', 'd']

and I need to pass these as parameters to a function:

我需要将这些作为参数传递给函数:

window.myFunction('a', 'b', 'c', 'd');

This would be easier if I could just pass the array/object into the function, but the functions are written by other people or already exist and I cannot change them - they need to be passed as individual parameters, which is what I need solved.

如果我可以将数组/对象传递给函数,这会更容易,但是这些函数是由其他人编写的或者已经存在并且我无法更改它们 - 它们需要作为单独的参数传递,这就是我需要解决的问题。

The number of values being passed is not consistent. It may be one, it may be 100.

传递的值数量不一致。可能是1,也可能是100。

Again, I cannot affect the functions. They are how they are, and I will always receive an array of values to pass into them.

同样,我不能影响功能。它们就是它们的样子,我总是会收到一组要传递给它们的值。

回答by Blue Skies

Use the .applymethod of the Function object.

使用.applyFunction 对象的方法。

window.myFunction.apply(window, ['a','b','c','d']);

The .applymethod invokes the function you're calling, but lets you set the function's thisvalue (the first argument) and lets you set its arguments using an Array (the second argument).

.apply方法调用您正在调用的函数,但允许您设置函数的this值(第一个参数)并允许您使用数组(第二个参数)设置其参数。

So here we kept windowas the thisvalue, and we're passing the Array as the individual arguments. The Array members will be distributed as though they were passed as individual arguments, so it's as if you had done this:

所以这里我们保留window作为this值,我们将数组作为单独的参数传递。Array 成员将被分发,就好像它们作为单独的参数传递一样,所以就好像你已经这样做了:

window.myFunction('a','b','c','d');

回答by hazerd

Try

尝试

window.myFunction.apply(window, ['a', 'b', 'c', 'd']);

回答by Mohammad Usman

In ES6 you can use spread syntax.

在 ES6 中,您可以使用扩展语法。

As from Docs:

文档

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

Spread 语法允许在需要零个或多个参数(用于函数调用)或元素(用于数组文字)的地方扩展诸如数组表达式或字符串之类的可迭代对象,或者在零个或多个地方扩展对象表达式键值对(用于对象文字)是预期的。

Your syntax will be:

您的语法将是:

var array = ['a', 'b', 'c', 'd'];
myFunction(...array);

Demo:

演示:

var array = ['a', 'b', 'c', 'd'];
myFunction(...array);

function myFunction(p1, p2, p3, p4) {
  console.log(p1, p2, p3, p4);
}

回答by Ronnie Royston

Array.prototype.map()

Array.prototype.map()

var arr = ['a','b','c'];

function echo(x){ 
  console.log(x)
}

function go(){
  arr.map(echo);
}
<button onclick="go()">Test</button>