多个值的Javascript索引

前端之家收集整理的这篇文章主要介绍了多个值的Javascript索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有像这样的多维 javascript数组.
[
["9","16","19","24","29","38"],["9","15",["10","17","39"],"21","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",//All the results which contain 15

]

2.例如检查数组是[15,21]
然后是多维数组

[
    ["9","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() method
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/

请注意,在版本9到you can fix that之前,IE不支持.filter().

猜你在找的JavaScript相关文章