多个值的Javascript索引

前端之家收集整理的这篇文章主要介绍了多个值的Javascript索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有像这样的多维 javascript数组.
  1. [
  2. ["9","16","19","24","29","38"],["9","15",["10","17","39"],"21","38"]
  3.  
  4. .......
  5. .......
  6. ]@H_502_3@
  7. 大概40左右

  8. 我正在使用另一个名为check的数组,其中包含以下值

  9. [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@H_502_3@ 
  10.  

    我想要的是我需要根据检查数组元素使多维数组唯一

  11.  

    1.例如检查数组是[15]
    然后是多维数组

  12.   
  13.  
    [
  14.     ["9",//All the results which contain 15
  15. ]@H_502_3@ 
  16.  

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

  17.   
  18.  
    [
  19.     ["9","38"]
  20.     //simply containing 15 AND 21.Please note prevIoUs example has only 15 in it not 21
  21.     //I need an AND not an OR
  22. ]@H_502_3@ 
  23.  

    我曾经尝试过JavaScript IndexOf方法BUt它给我一个OR结果而不是AND

  24.  

    提前致谢

解决方法

你可以使用 .filter() method
  1. var mainArray = [ /* your array here */ ],check = ["15","21"],result;
  2.  
  3. result = mainArray.filter(function(val) {
  4. for (var i = 0; i < check.length; i++)
  5. if (val.indexOf(check[i]) === -1)
  6. return false;
  7. return true;
  8. });
  9.  
  10. console.log(result);@H_502_3@
  11. 演示:http://jsfiddle.net/nnnnnn/Dq6YR/

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

猜你在找的JavaScript相关文章