2

I want a way to pass multiple arrays as function parameters (dynamically), and store their values into a single array (I know how to do this part) as demonstrated bellow:

function arrayfunction (/*arrays go here*/) {
  var allArrays = []
}
arrayfunction([a,b,c],[d,e,f],[h,i,j],...);

how can that be done?

Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
dadadodo
  • 349
  • 2
  • 15

2 Answers2

2

this should get you started

function arrayfunction (/*arrays go here*/) 
{
  var outputArr = [];
  for ( var counter = 0; counter < arguments.length; counter++)
  {
    outputArr = outputArr.concat( arguments[ counter ] );
  }
  return outputArr;
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You can use Rest Parameters if you are using babel or other transpiler which supports ES6. For ES5 you can use arguments keyword to get arguments passed to the function, but be ware that arguments is not an array, and you need to do something like var args = Array.prototype.slice.call(arguments, 1); to get an array.

Pavel Staselun
  • 1,970
  • 1
  • 12
  • 24