如何使indexof()区分数组中的两个相似数字

时间:2016-10-17 11:17:40

标签: javascript arrays

我有一个数组说

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

我正在使用javascript indexof()来检查数组中是否存在元素,以及它是否不会退出以执行某些操作....

我的问题是,当一个元素如1,2,3,4,5,6在数组中不存在时,它仍然判断元素如10为1,12为2,13为3,14为4,15为5,16为6。 所以它仍然会返回该元素存在,尽管它不存在

例如:

if x = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

这仍然无效,因为它会将10视为1

if(data1.indexOf(1)===-1){											document.getElementById("s1").style.backgroundColor= "red";
    $("input[name='seat1']").attr("disabled", "disabled");
}

4 个答案:

答案 0 :(得分:1)

您可以parse字符串并使用indexOf作为数组方法。

JSON.parse(data1).indexOf(1) ...

答案 1 :(得分:0)

Array.indexOf适用于任何浏览器> IE8:MDN Browser compatibility

Polyfill如果您需要支持任何内容,请填写。

// POLYFILL TO SUPPORT ANY BROWSER
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {

    var k;

    // 1. Let o be the result of calling ToObject passing
    //    the this value as the argument.
    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }

    var o = Object(this);

    // 2. Let lenValue be the result of calling the Get
    //    internal method of o with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = o.length >>> 0;

    // 4. If len is 0, return -1.
    if (len === 0) {
      return -1;
    }

    // 5. If argument fromIndex was passed let n be
    //    ToInteger(fromIndex); else let n be 0.
    var n = +fromIndex || 0;

    if (Math.abs(n) === Infinity) {
      n = 0;
    }

    // 6. If n >= len, return -1.
    if (n >= len) {
      return -1;
    }

    // 7. If n >= 0, then Let k be n.
    // 8. Else, n<0, Let k be len - abs(n).
    //    If k is less than 0, then let k be 0.
    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

    // 9. Repeat, while k < len
    while (k < len) {
      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the
      //    HasProperty internal method of o with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      //    i.  Let elementK be the result of calling the Get
      //        internal method of o with the argument ToString(k).
      //   ii.  Let same be the result of applying the
      //        Strict Equality Comparison Algorithm to
      //        searchElement and elementK.
      //  iii.  If same is true, return k.
      if (k in o && o[k] === searchElement) {
        return k;
      }
      k++;
    }
    return -1;
  };
}

// TEST ARRAY
var arr = [0,1,2,3,4,6,7,8,9,10,12,13,14,16,17,18,20]
// TESTS
var offset = 0;
for(var i = 0; i <= 20; i++) {
  if(arr.indexOf(i) === -1) {
    console.log(i, "not found");
  } else  {
    console.log(i, "found")
  }
}

答案 2 :(得分:0)

我会将它们转换为这样的字符串。

function existsInArray(numberArrays, number){
       const numAsStrings = numberArrays.join(',').split(',')
       return numAsStrings.indexOf(String(number)) > -1
    }
    
    console.log(existsInArray([1,2,3,4,5,6,33], 33))
    //true
    
    console.log(existsInArray([1,2,3,4,5,6,33], 34))
    //false

希望这有帮助。

答案 3 :(得分:0)

您的示例代码段无用,因为它不会显示您正在处理的数据(“data1”)。

您描述的行为不是您的示例数组所获得的行为:

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

alert("indexOf(1):" + x.indexOf(1));
alert("indexOf(10):" + x.indexOf(10));
alert("indexOf(0):" + x.indexOf(0));

它按预期工作。

您的真实数据可能包含字符串而不是整数。