1

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.

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? 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 come from, you can certainly use a loop to only code a single call to myFunction. Commented Sep 12, 2016 at 13:48

3 Answers 3

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);
});
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.