多个值的 Javascript 索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11881859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:39:24  来源:igfitidea点击:

Javascript index of for multiple values

javascriptloopsfor-loopindexing

提问by coolguy

Im having a multdimensional javascript arrays like this.

我有一个像这样的多维 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"]

.......
.......
]

Around 40 to be exact

准确的说是40左右

and im having an another array called check with the following values

我有一个名为 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

What i want is i need to make the multidimensional array unique based for the check array elements

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

1.Example if check array is [15]Then multidimensional array would be

1.Example if check array is [15]then multidimensional array will be

[
    ["9", "15", "19", "24", "29", "38"],
   //All the results which contain 15

]

2.Example if check array is [15,21]Then multidimensional array would be

2.Example if check array is [15,21]then multidimensional array will be

[
    ["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

]

I had tried the JavaScript IndexOf method BUt it is getting me an OR'ed result not AND

我曾尝试过 JavaScript IndexOf 方法,但它给我的结果不是 AND

Thanks in Advance

提前致谢

采纳答案by nnnnnn

You can use the .filter()method:

您可以使用以下.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);

Demo: http://jsfiddle.net/nnnnnn/Dq6YR/

演示:http: //jsfiddle.net/nnnnnn/Dq6YR/

Note that .filter()is not supported by IE until version 9, but you can fix that.

请注意,.filter()直到版本 9 才支持 IE,但您可以修复该.

回答by pimvdb

Using .filterand .every, you can filter the rows for which every argument is apparent in the row:

使用.filterand .every,您可以过滤行中每个参数都显而易见的行:

var filter = function(args) {
  return arr.filter(function(row) {
    return args.every(function(arg) {
      return ~row.indexOf(String(arg));  // [15] should search for ["15"]
    });
  });
};

.everyeffectively does an ANDoperation here; for ORyou could use .someinstead. Those array functions are available on newer browsers.

.everyAND在这里有效地进行操作;因为OR你可以.some改用。这些数组函数在较新的浏览器上可用。

回答by Amberlamps

There is also a solution that is not using filter()or every():

还有一个不使用filter()or的解决方案every()

// function

function AndArray(start, check) {

    this.stripArray = function(element, array) {
        var _array = [];
        for(var i = 0; i < array.length; i++)
            for(var j = 0; j < array[i].length; j++)
                if(element == array[i][j]) _array.push(array[i]);
        return _array;
        }

    for(var k = 0; k < start.length; k++)
        if(start.length > 0) start = this.stripArray(check[k], start);

    return start;

    }

// use

var check = [15, 21];

var start= [[ 15, 6 , 8], [3, 21, 56], [15, 3, 21]];

console.log(AndArray(start, check));    

回答by Terrel Jones Jr.

This should do it

这应该做

var checkURL = function (url) {
  var final = false;
  if (url.indexOf('jpg') > -1) {
    final = true
  }

  if (url.indexOf('jpeg') > -1) {
    final = true
  }

  if (url.indexOf('gif') > -1) {
    final = true
  }

  if (url.indexOf('png') > -1) {
    final = true
  }

  return final
}