Best way to find an item in a JavaScript Array ?

Posted by zimbatm on Stack Overflow See other posts from Stack Overflow or by zimbatm
Published on 2008-09-27T15:41:12Z Indexed on 2010/03/27 0:43 UTC
Read the original article Hit count: 258

Filed under:
|

What is the best way to find if an object is in an array ?

This is the best way I know:

function include(arr, obj) {
  for(var i=0; i<arr.length; i++) {
    if (arr[i] == obj) return true;
  }
}

include([1,2,3,4], 3); // true
include([1,2,3,4], 6); // undefined

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays