admin 管理员组

文章数量: 1086019

Let's say I have this function

function myFunction(a, b, c) {
    ... code here ...
}

Now I want to call it multiple times with several different arguments each time:

myFunction('sky', 'blue', 'air');
myFunction('tree', 'green', 'leaf');
myFunction('sun', 'yellow', 'light');
myFunction('fire', 'orange', 'heat');
myFunction('night', 'black', 'cold');

How can I merge all those calls in just one?

I know how to do it with iterations or forEach when there is just one argument, but I can't figure out how to do it with various non-numerical arguments.

Let's say I have this function

function myFunction(a, b, c) {
    ... code here ...
}

Now I want to call it multiple times with several different arguments each time:

myFunction('sky', 'blue', 'air');
myFunction('tree', 'green', 'leaf');
myFunction('sun', 'yellow', 'light');
myFunction('fire', 'orange', 'heat');
myFunction('night', 'black', 'cold');

How can I merge all those calls in just one?

I know how to do it with iterations or forEach when there is just one argument, but I can't figure out how to do it with various non-numerical arguments.

Share Improve this question asked Sep 12, 2016 at 13:46 StealthStealth 1552 silver badges9 bronze badges 2
  • 1 That depends on your parameters, are they stored in an array? are they based on user input? Can you give some more context? – roberrrt-s Commented Sep 12, 2016 at 13:47
  • 1 "How can I merge all those calls in just one?" You can't, if you mean a single call to myFunction, not without changing how myFunction works. Depending on where the values e from, you can certainly use a loop to only code a single call to myFunction. – T.J. Crowder Commented Sep 12, 2016 at 13:48
Add a ment  | 

3 Answers 3

Reset to default 5

Unless you change myFunction, you can't call it once and have it magically act as though it had been called five times.

You can code a single call in a loop, though, if the information you're for the arguments using is stored elsewhere.

For instance, if we assume an array of objets:

var data = [
    {a: 'sky', b: 'blue', c: 'air'},
    {a: 'tree', b: 'green', c: 'leaf'},
    {a: 'sun', b: 'yellow', c: 'light'},
    {a: 'fire', b: 'orange', c: 'heat'},
    {a: 'night', b: 'black', c: 'cold'}
]:

then

data.forEach(function(entry) {
    myFunction(entry.a, entry.b, entry.c);
});

Or if it's an array of arrays, we can use the nifty Function#apply function:

var data = [
    ['sky', 'blue', 'air'],
    ['tree', 'green', 'leaf'],
    ['sun', 'yellow', 'light'],
    ['fire', 'orange', 'heat'],
    ['night', 'black', 'cold']
]:

then:

data.forEach(function(entry) {
    myFunction.apply(null, entry);
});

You seem to be looking for apply

var values = [
    ['sky', 'blue', 'air'],
    ['tree', 'green', 'leaf'],
    ['sun', 'yellow', 'light'],
    ['fire', 'orange', 'heat'],
    ['night', 'black', 'cold']
];
values.forEach(function(args) { myFunction.apply(null, args); })

or spread syntax in ES6:

for (const args of values) myFunction(...args);

lets suppose you already have an array with all the values:

var colors = ['sky', 'blue', 'air', 'tree', 'green', 'leaf', 'sun', 'yellow', 'light', 'fire', 'orange', 'heat', 'night', 'black', 'cold'];

while(colors.length > 0){
  myFunction.apply(this, colors.splice(0,3)); //call the function with chunks of 3 elements
}

本文标签: Call a function multiple times with more than one argument in JavaScriptStack Overflow