2015-06-21 56 views
1

我目前正在检查传递给JavaScript函数的参数中的数组。 参数可以为类型:检查参数中是否存在数组

1. function(a, b, c, d) 
2. function([a, b, c, d]) 
3. function([a], b, c, [d, e, f], g) 

我需要检查,如果参数包含在单个阵列或没有。下面的代码工作的情况下1.2.但不是3.

if (Array.isArray(args)){ 
    // section A 
} 
else{ 
    // section B 
} 

此代码是考虑3.是一个数组,虽然它混合值,它正在进入的,而不是B.一节中,我希望它进入B节。只有围绕着[]的参数才能完全进入A.

+0

没有我的回答还是不行你想要什么? – AmmarCSE

回答

1

要么唯一的参数是一个数组或没有的参数是一个数组:

function foo(args) 
{ 
    var v; 

    if (arguments.length === 1 && Array.isArray(args)) { 
     v = args; // grab vector (B) 
    } else if (arguments.length >= 1 && ![].some.call(arguments, Array.isArray)) { 
     v = [].slice.call(arguments, 0); (A) 
    } else { 
     throw "begone, evil caller"; 
    } 
    // do your stuff here 
} 
1

您可以使用arguments对象。迭代通过arguments对象并检查方式通

test(1, 2, 3, 4); 
 
test([1, 2, 3, 4]); 
 
test([1], 2, 3, [4, 5, 6], 7); 
 

 
function test() { 
 
    var onlyArray = true; 
 
    for (var i = 0; i < arguments.length; i++) { 
 
    if (!Array.isArray(arguments[i])) { 
 
     onlyArray = false; 
 
     break; 
 
    } 
 
    } 
 
    if (onlyArray) { 
 
     snippet.log('section A'); 
 
     // section A 
 
    } else { 
 
     snippet.log('section B'); 
 
     // section B 
 
    } 
 
}
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

1

根据您的更新问题

每个参数看到再次更新jsfiddle

function containsOneArray(test) { 
    return test.length === 1 && Array.isArray(test[0]); 
} 

function yourFunction() { 
    if(containsOneArray(arguments)) { 
     console.log(true); 
    } else { 
     console.log(false); 
    } 
} 

yourFunction(['hello']); // true 
yourFunction(['i', 'am', 'cool']); // true 
yourFunction('hello'); // false 
yourFunction(['a'], 'b', 'c', ['d', 'e', 'f'], 'g'); // false 

新建答案

添加了一些分离的关注(见jsfiddle):

function containsArray(_args) { 
    var args = Array.prototype.slice.call(_args), 
     contains = false; 

    args.forEach(function(arg) { 
     if(Array.isArray(arg)) { 
      contains = true; 
      return; // don't need to keep looping 
     } 
    }); 

    return contains; 
} 

function yourFunction() { 
    if(containsArray(arguments)) { 
     console.log(true); 
    } else { 
     console.log(false); 
    } 
} 

yourFunction(['hello']); // true 
yourFunction('hello'); // false 
yourFunction(['a'], 'b', 'c', ['d', 'e', 'f'], 'g'); // true 

它的作用是什么给你一个效用函数只检查是否传递到yourFunctionarguments对象包含Array任何地方。

老回答

退房的jsfiddle

function containsArray() { 
    var args = Array.prototype.slice.call(arguments), 
     contains = false; 

    args.forEach(function(arg) { 
     if(Array.isArray(arg)) { 
      contains = true; 
      return; // don't need to keep looping 
     } 
    }); 

    console.log(contains); 

    if(contains) { 
     // do something 
    } else { 
     // do something else 
    } 
} 

containsArray(['hello']); // true 
containsArray('hello'); // false 
containsArray(['a'], 'b', 'c', ['d', 'e', 'f'], 'g'); // true 
+0

我实际上需要为第三种情况返回'false'。仅当所有参数都在一个数组中时才需要返回“true”。 –