Answer by cocco for How do I check if an array includes a value in JavaScript?
A hopefully faster bidirectional indexOf / lastIndexOf alternative 2015 While the new method includes is very nice, the support is basically zero for now. It's long time that I was thinking of way to...
View ArticleAnswer by AlonL for How do I check if an array includes a value in JavaScript?
One-liner: function contains(arr, x) { return arr.filter(function(elem) { return elem == x }).length > 0; }
View ArticleAnswer by Oriol for How do I check if an array includes a value in JavaScript?
ECMAScript 7 introduces Array.prototype.includes. It can be used like this: [1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false It also accepts an optional second argument fromIndex: [1, 2,...
View ArticleAnswer by dansalmo for How do I check if an array includes a value in...
function contains(a, obj) { return a.some(function(element){return element == obj;}) } Array.prototype.some() was added to the ECMA-262 standard in the 5th edition
View ArticleAnswer by dr.dimitru for How do I check if an array includes a value in...
We use this snippet (works with objects, arrays, strings): /* * @function * @name Object.prototype.inArray * @description Extend Object prototype within inArray function * * @param {mix} needle -...
View ArticleAnswer by Michael for How do I check if an array includes a value in JavaScript?
The top answers assume primitive types but if you want to find out if an array contains an object with some trait, Array.prototype.some() is a very elegant solution: const items = [ {a: '1'}, {a: '2'},...
View ArticleAnswer by Eduardo Cuomo for How do I check if an array includes a value in...
I use the following: Array.prototype.contains = function (v) { return this.indexOf(v) > -1; } var a = [ 'foo', 'bar' ]; a.contains('foo'); // true a.contains('fox'); // false
View ArticleAnswer by Pradeep Mahdevu for How do I check if an array includes a value in...
ECMAScript 6 has an elegant proposal on find. The find method executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such...
View ArticleAnswer by Mina Gabriel for How do I check if an array includes a value in...
Use: var myArray = ['yellow', 'orange', 'red'] ; alert(!!~myArray.indexOf('red')); //true Demo To know exactly what the tilde ~ do at this point, refer to this question What does a tilde do when it...
View ArticleAnswer by Matías Cánepa for How do I check if an array includes a value in...
Use: function isInArray(array, search) { return array.indexOf(search) >= 0; } // Usage if(isInArray(my_array, "my_value")) { //... }
View ArticleAnswer by stamat for How do I check if an array includes a value in JavaScript?
I looked through submitted answers and got that they only apply if you search for the object via reference. A simple linear search with reference object comparison. But lets say you don't have the...
View ArticleAnswer by Simon_Weaver for How do I check if an array includes a value in...
As others have mentioned you can use Array.indexOf, but it isn't available in all browsers. Here's the code from...
View ArticleAnswer by Andy Rohr for How do I check if an array includes a value in...
Similar thing: Finds the first element by a "search lambda": Array.prototype.find = function(search_lambda) { return this[this.map(search_lambda).indexOf(true)]; }; Usage:...
View ArticleAnswer by LmC for How do I check if an array includes a value in JavaScript?
function inArray(elem,array) { var len = array.length; for(var i = 0 ; i < len;i++) { if(array[i] == elem){return i;} } return -1; } Returns array index if found, or -1 if not found
View ArticleAnswer by ninjagecko for How do I check if an array includes a value in...
While array.indexOf(x)!=-1 is the most concise way to do this (and has been supported by non-Internet Explorer browsers for over decade...), it is not O(1), but rather O(N), which is terrible. If your...
View ArticleAnswer by william malo for How do I check if an array includes a value in...
b is the value, and a is the array. It returns true or false: function(a, b) { return a.indexOf(b) != -1 }
View ArticleAnswer by Carlos A for How do I check if an array includes a value in...
Use: Array.prototype.contains = function(x){ var retVal = -1; // x is a primitive type if(["string","number"].indexOf(typeof x)>=0 ){ retVal = this.indexOf(x);} // x is a function else if(typeof x...
View ArticleAnswer by Ekim for How do I check if an array includes a value in JavaScript?
Literally: (using Firefox v3.6, with for-in caveats as previously noted (HOWEVER the use below might endorse for-in for this very purpose! That is, enumerating array elements that ACTUALLY exist via a...
View ArticleAnswer by Ztyx for How do I check if an array includes a value in JavaScript?
If you are checking repeatedly for existence of an object in an array you should maybe look into Keeping the array sorted at all times by doing insertion sort in your array (put new objects in on the...
View ArticleAnswer by Dennis Allen for How do I check if an array includes a value in...
Just another option // usage: if ( ['a','b','c','d'].contains('b') ) { ... } Array.prototype.contains = function(value){ for (var key in this) if (this[key] === value) return true; return false; } Be...
View Article