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 :-)
doSomething(...arr).