coo*_*guy 6 javascript indexing loops for-loop
我有像这样的多维javascript数组.
[
["9", "16", "19", "24", "29", "38"],
["9", "15", "19", "24", "29", "38"],
["10", "16", "19", "24", "29", "38"],
["9", "16", "17", "19", "24", "29", "39"],
["10", "16", "17", "19", "24", "29", "39"],
["9", "15", "21", "24", "29", "38"]
.......
.......
]
大概40左右
我正在使用另一个名为check的数组,其中包含以下值
 [9,10] //This is of size two for example,it may contain any number of elements BUT it only contains elements(digits) from the multidimensional array above
我想要的是我需要根据检查数组元素使多维数组唯一
1.例如,如果检查数组是[15]
那么多维数组就是
[
    ["9", "15", "19", "24", "29", "38"],
   //All the results which contain 15
]
2.例如,如果检查数组是[15,21]
那么多维数组就是
[
    ["9", "15", "21", "24", "29", "38"]
    //simply containing 15 AND 21.Please note previous example has only 15 in it not 21
    //I need an AND not an OR
]
我曾经尝试过JavaScript IndexOf方法BUt它给我一个OR结果而不是AND
提前致谢
您可以使用以下.filter()方法:
var mainArray = [ /* your array here */ ],
    check = ["15", "21"],
    result;
result = mainArray.filter(function(val) {
    for (var i = 0; i < check.length; i++)
        if (val.indexOf(check[i]) === -1)
            return false;    
    return true;
});
console.log(result);
演示:http://jsfiddle.net/nnnnnn/Dq6YR/
请注意,.filter()在版本9之前,IE不支持,但您可以解决此问题.