07.20.08

Javascript version of php in_array

Posted in General Javascript | at 2:59 am by admin

I was working with javascript arrays and needed to find a needle in a haystack or a value in an array. I came across this function:-

function js_in_array(the_needle, the_haystack){
   var the_hay = the_haystack.toString();
   if(the_hay == ''){
      return false;
   }
   var the_pattern = new RegExp(the_needle, 'g');
   var matched = the_pattern.test(the_haystack);
   return matched;
}

Leave a Comment

You must be logged in to post a comment.