Check if an array contains an item by using jQuery's inArray
:
js
var a = [1, 2];
$.inArray(a, 2); => 1 // returns the index
$.inArray(a, 3); => -1 // when not found
Use the indexOf
of underscore:
js
_.indexOf(2, a); => 1
_.indexOf(3, a); => -1
If you're not supporting older IE browsers (pre IE9), you can use the native indexOf
:
js
a.indexOf(2); => 1
a.indexOf(3); => -1