[javascript] Javascript - check array for value

I have a simple array with bank holidays:

var bank_holidays = ['06/04/2012','09/04/2012','07/05/2012','04/06/2012','05/06/2012','27/08/2012','25/12/2012','26/12/2012','01/01/2013','29/03/2013','01/04/2013','06/05/2013','27/05/2013'];

I want to do a simple check to see if certain dates exist as part of that array, I have tried:

if('06/04/2012' in bank_holidays) { alert('LOL'); }
if(bank_holidays['06/04/2012'] !== undefined) { alert 'LOL'; }

And a few other solutions with no joy, I have also tried replacing all of the forwarded slashes with a simple 'x' in case that was causing issues.

Any recommendations would be much appreciated, thank you!

(edit) Here's a jsFiddle - http://jsfiddle.net/ENFWe/

This question is related to javascript arrays

The answer is


This should do it:

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

jsFiddle


Try this:

// 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!
}