假设我有这个:

var blockedTile = new Array("118", "67", "190", "43", "135", "520");

还有更多的数组元素,但出于可读性考虑,这些元素很少。无论如何,我可以做一个“for”循环,但每次你点击地图时,它会做500个循环……有没有其他方法来查看某个字符串是否在数组中?


当前回答

if(array.indexOf("67") != -1) // is in array

其他回答

在数组的例子中,它在php (in_array)中是一样的

 var ur_fit = ["slim_fit", "tailored", "comfort"];
 var ur_length = ["length_short", "length_regular", "length_high"];
    if(ur_fit.indexOf(data_this)!=-1){
        alert("Value is avail in ur_fit array");
    }
    else if(ur_length.indexOf(data_this)!=-1){      
         alert("value is avail in ur_legth array"); 

    }

一些浏览器支持Array.indexOf()。

如果不是,你可以通过它的原型来扩充Array对象,就像这样…

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(searchElement /*, fromIndex */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;

    var n = 0;
    if (arguments.length > 0)
    {
      n = Number(arguments[1]);
      if (n !== n) // shortcut for verifying if it's NaN
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    if (n >= len)
      return -1;

    var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);

    for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
}

源。

var myArray = [2,5,6,7,9,6];
myArray.includes(2) // is true
myArray.includes(14) // is false

我这边的一小段代码(数组的自定义函数):

    Array.prototype.in_array = function (array) {
        var $i = 0;
        var type = typeof array;
        while (this[$i]) {
            if ((type == ('number') || type == ('string'))  && array == this[$i]) {
                return true;
            } else if (type == 'object' && array instanceof Array && array.in_array(this[$i])) {
                return true
            }
            $i++;
        }
        return false;
    };


    var array = [1, 2, 3, "a", "b", "c"];

    //if string in array
    if (array.in_array('b')) {
        console.log("in array");
    }

    //if number in array
    if (array.in_array(3)) {
        console.log("in array");
    }

    // if one from array in array
    if (array.in_array([1, 'b'])) {
        console.log("in array");
    }

Assuming that you're only using the array for lookup, you can use a Set (introduced in ES6), which allows you to find an element in O(1), meaning that lookup is sublinear. With the traditional methods of .includes() and .indexOf(), you still may need to look at all 500 (ie: N) elements in your array if the item specified doesn't exist in the array (or is the last item). This can be inefficient, however, with the help of a Set, you don't need to look at all elements, and instead, instantly check if the element is within your set:

const blockedTile = new集((“118”、“67”、“190”、“43”、“135”、“520”)); 如果(blockedTile.has (" 118 ")) { // 118在你的Set中 console.log(“发现118”); }

如果出于某种原因需要将集合转换回数组,可以通过使用array .from()或扩展语法(…)来实现,但是,这将遍历整个集合的内容(将是O(N))。集合也不会保留重复项,这意味着数组不会包含重复项。