Looping through unknown number of array arguments

后端 未结 4 1316
暖寄归人
暖寄归人 2020-12-17 21:20

I am trying to figure out how to loop through several array arguments passed. 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 functi

4条回答
  •  清歌不尽
    2020-12-17 21:56

    Use forEach, as 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);
    

提交回复
热议问题