Modern browsers have
jQuery has a utility function for this:
jQuery has several useful utility functions.
An excellent JavaScript utility library is underscore.js:
Array#indexOf
, which does exactly that; this is in the new(ish) ECMAScript 5th edition specification, but it has been in several browsers for years. Older browsers can be supported using the code listed in the "compatibility" section at the bottom of that page.jQuery has a utility function for this:
$.inArray(value, array)
It returns the index of a value in an array. It returns -1 if the array does not contain the value.jQuery has several useful utility functions.
An excellent JavaScript utility library is underscore.js:
_.contains(list, value)
, alias_.include(list, value)
(underscore's contains/include uses indexOf internally if passed a JavaScript array).
- Dojo Toolkit:
dojo.indexOf(array, value, [fromIndex, findLast])
documentation. Dojo has a lot of utility functions, see http://api.dojotoolkit.org. - Prototype:
array.indexOf(value)
documentation - MooTools:
array.indexOf(value)
documentation - MochiKit:
findValue(array, value)
documentation - MS Ajax:
array.indexOf(value)
documentation - Ext:
Ext.Array.indexOf(array, value, [from])
documentation
Languages that compile to javascript
In coffeescript, thein
operator is the equivalent of contains
:a = [1, 2, 3, 4]
alert(2 in a)
Dart:var mylist = [1, 2, 3];
assert(mylist.contains(1));
assert(mylist.indexOf(1) == 0);
0 comments:
Post a Comment