0

In JavaScript, it is pretty easy now to make a function accept a variable number of arguments:

function doSomething(...args) {
    args.forEach(arg => console.log(arg));
}

Now it can be called like doSomething(1, 2, 3) and all arguments will be available inside the function as array args. The output will be:

1
2
3

Now I want to call the function, passing all values in one array, like this:

const arr = [1, 2, 3];
doSomething(arr);

and have the same result. To do it, I have to use lodash's _.flatten inside the function:

doSomething(...args) {
    args = _.flatten(args);
    ...
}

Are there better ways to modify my function to do this?

I don't need any solution, I already have one. I need good solutions doing exactly what I need, but without third party libraries like Lodash and still elegant. I ask because of curiosity, not because I don't know how to do that at all :-)

5
  • If you know it is an array, use doSomething(...arr). Commented May 30, 2017 at 12:27
  • @str, it's a useful tip, but not exactly what I need. Commented May 30, 2017 at 12:30
  • you can use default arguments developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 30, 2017 at 12:31
  • @Dinesh Can you expand that to an answer? Commented May 30, 2017 at 12:42
  • @MikhailBatcer check Shakti Phartiyal answer Commented May 30, 2017 at 12:43

2 Answers 2

1

Take a look at apply:

function doSomething (...args) {
  args.forEach(arg => console.log(arg));
}

const arr = [1, 2, 3];
doSomething.apply(null, arr);

Or check if the first argument is an array:

function doSomething () {
  let args;
  if (Array.isArray(arguments[0])) {
    args = arguments[0];
  } else {
    args = Array.slice(argument);
  }
  args.forEach(arg => console.log(arg));
}

const arr = [1, 2, 3];
doSomething.apply(null, arr);

This approach however is a bit more verbose and doesn't make use of the spread operator. Also, things like this would not work:

const arr = [[1, 2], [3, 4]];
doSomething.apply(null, arr);
Sign up to request clarification or add additional context in comments.

3 Comments

doSomething.apply(null, arr) isn't doSomething(arr), but it's interesting approach.
If I omit ..., the function won't support a call with arguments passed like (1, 2, 3), with variable number of them.
Yes you are perfectly right. I didn't think about that when writing the comment.
1

If you don't want to flatten all arrays but only use one, then the following should do:

if (Array.isArray(args[0])) args = args[0];

You might also want to check for args.length == 1 in that case.

But in general, instead of overloading your function to do different things with different numbers or types arguments, it's much easier and safer to provide multiple functions:

function doSomething(...args) {
    // implementation
}
function doSomethingArr(arr) {
    return doSomething(...arr);
}

or

function doSomething(...args) {
    return doSomethingArr(args);
}
function doSomethingArr(arr) {
    // implementation
}

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.