Quoting for an unknown number of array arguments

I am trying to figure out how to skip a few array arguments. For example: [1,2,3,4,5], [3,4,5], [5,6,7] If I pass it to a function, how would I have a function loop inside each argument (any number of arrays can to be transferred)?

I want to use the for loop here.

+8
javascript
source share
3 answers

You can use arguments for this:

for(var arg = 0; arg < arguments.length; ++ arg) { var arr = arguments[arg]; for(var i = 0; i < arr.length; ++ i) { var element = arr[i]; /* ... */ } } 
+12
source share

Use the built-in arguments keyword, which will contain the length of how many arrays you have. Use this as the basis for scrolling through each array.

+2
source share

Use forEach as shown below:

'use strict';

 function doSomething(p1, p2) { var args = Array.prototype.slice.call(arguments); args.forEach(function(element) { console.log(element); }, this); } doSomething(1); doSomething(1, 2); 
0
source share

All Articles