Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value?

This is the only way I know to do it:

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

Is there a better and more concise way to accomplish this?

like image 226
brad Avatar asked Oct 25 '08 22:10

brad


People also ask

How do you check if an array contains a value in JavaScript?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

How do you check if an array does not contain a value JavaScript?

To check if an array doesn't include a value, use the logical NOT (!) operator to negate the call to the includes() method. The NOT (!) operator returns false when called on a true value and vice versa.

How do you check if an array contains a value from another array JavaScript?

Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.


2 Answers

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:

console.log(['joe', 'jane', 'mary'].includes('jane')); //true

You can also use Array#indexOf, which is less direct, but doesn't require polyfills for outdated browsers.

console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true

Many frameworks also offer similar methods:

  • jQuery: $.inArray(value, array, [fromIndex])
  • Underscore.js: _.contains(array, value) (also aliased as _.include and _.includes)
  • Dojo Toolkit: dojo.indexOf(array, value, [fromIndex, findLast])
  • Prototype: array.indexOf(value)
  • MooTools: array.indexOf(value)
  • MochiKit: findValue(array, value)
  • MS Ajax: array.indexOf(value)
  • Ext: Ext.Array.contains(array, value)
  • Lodash: _.includes(array, value, [from]) (is _.contains prior 4.0.0)
  • Ramda: R.includes(value, array)

Notice that some frameworks implement this as a function, while others add the function to the array prototype.

like image 69
37 revs, 22 users 18% Avatar answered Sep 28 '22 15:09

37 revs, 22 users 18%


Update from 2019: This answer is from 2008 (11 years old!) and is not relevant for modern JS usage. The promised performance improvement was based on a benchmark done in browsers of that time. It might not be relevant to modern JS execution contexts. If you need an easy solution, look for other answers. If you need the best performance, benchmark for yourself in the relevant execution environments.

As others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. So you may want to rewrite your code as follows:

function contains(a, obj) {     var i = a.length;     while (i--) {        if (a[i] === obj) {            return true;        }     }     return false; } 

Of course, you may as well extend Array prototype:

Array.prototype.contains = function(obj) {     var i = this.length;     while (i--) {         if (this[i] === obj) {             return true;         }     }     return false; } 

And now you can simply use the following:

alert([1, 2, 3].contains(2)); // => true alert([1, 2, 3].contains('2')); // => false 
like image 44
Damir Zekić Avatar answered Sep 28 '22 15:09

Damir Zekić