Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环遍历未知数量的数组参数_Javascript - Fatal编程技术网

Javascript 循环遍历未知数量的数组参数

Javascript 循环遍历未知数量的数组参数,javascript,Javascript,我试图找出如何循环传递的几个数组参数。 例如:[1,2,3,4,5],[3,4,5],[5,6,7] 如果我把它传递给一个函数,我如何在每个参数中有一个函数循环(可以传递任意数量的数组) 我想在这里使用for循环 使用内置的参数关键字,该关键字将包含有多少数组的长度。以此为基础在每个数组中循环。您可以使用: for(var arg = 0; arg < arguments.length; ++ arg) { var arr = arguments[arg]; for(va

我试图找出如何循环传递的几个数组参数。 例如:[1,2,3,4,5],[3,4,5],[5,6,7] 如果我把它传递给一个函数,我如何在每个参数中有一个函数循环(可以传递任意数量的数组)


我想在这里使用for循环

使用内置的
参数
关键字,该关键字将包含有多少数组的
长度
。以此为基础在每个数组中循环。

您可以使用:

for(var arg = 0; arg < arguments.length; ++ arg)
{
    var arr = arguments[arg];

    for(var i = 0; i < arr.length; ++ i)
    {
         var element = arr[i];

         /* ... */
    } 
}
for(var arg=0;arg
使用forEach,如下所示:

'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);
函数loopthroughguments(){
//在函数顶部声明东西总是好的,
//加快查找速度!
var i=0,
len=arguments.length;
//请注意,for语句缺少初始化。
//初始化已定义,
//因此,无需为每次迭代不断定义。
对于(;i
在函数中,您将迭代
参数
,对于每个参数,您将迭代数组。var args=arguments的可能重复;对于(i=0;ifunction loopThroughArguments(){ // It is always good to declare things at the top of a function, // to quicken the lookup! var i = 0, len = arguments.length; // Notice that the for statement is missing the initialization. // The initialization is already defined, // so no need to keep defining for each iteration. for( ; i < len; i += 1 ){ // now you can perform operations on each argument, // including checking for the arguments type, // and even loop through arguments[i] if it's an array! // In this particular case, each argument is logged in the console. console.log( arguments[i] ); } };