check array for value

This question already has an answer here:

  • How do I check if an array includes an object in JavaScript? 40 answers

  • If you don't care about legacy browsers:

    if ( bank_holidays.indexOf( '06/04/2012' ) > -1 )
    

    if you do care about legacy browsers, there is a shim available on MDN. Otherwise, jQuery provides an equivalent function:

    if ( $.inArray( '06/04/2012', bank_holidays ) > -1 )
    

    尝试这个:

    // this will fix old browsers
    if (!Array.prototype.indexOf) {
      Array.prototype.indexOf = function(value) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] === value) {
            return i;
          }
        }
    
        return -1;
      }
    }
    
    // example
    if ([1, 2, 3].indexOf(2) != -1) {
      // yay!
    }
    

    This should do it:

    for (var i = 0; i < bank_holidays.length; i++) {
        if (bank_holidays[i] === '06/04/2012') {
            alert('LOL');
        }
    }
    

    jsFiddle

    链接地址: http://www.djcxy.com/p/13040.html

    上一篇: 检查数组中是否存在值

    下一篇: 检查数组的值