2015-06-21 82 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」。 –