Javascript - check array for value

arrays, javascript

Solution

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 )

Problem

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/

Original source

Related problems