The function I am using now to check this is the following:
function inArray(needle,haystack) { var count=haystack.length; for(var i=0;i<count;i++) { if(haystack[i]===needle){return true;} } return false; } It works. What I'm looking for is whether there is a better way of doing this.
49 Answers
ECMAScript 2016 incorporates an includes() method for arrays that specifically solves the problem, and so is now the preferred method.
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching) As of JULY 2018, this has been implemented in almost all major browsers, if you need to support an older browser a polyfill is available.
Edit: Note that this returns false if the item in the array is an object. This is because similar objects are two different objects in JavaScript.
12Code:
function isInArray(value, array) { return array.indexOf(value) > -1; } Execution:
isInArray(1, [1,2,3]); // true Update (2017):
In modern browsers which follow the ECMAScript 2016 (ES7) standard, you can use the function Array.prototype.includes, which makes it way more easier to check if an item is present in an array:
const array = [1, 2, 3]; const value = 1; const isInArray = array.includes(value); console.log(isInArray); // true7Just use indexOf:
haystack.indexOf(needle) >= 0 If you want to support old Internet Explorers (< IE9), you'll have to include your current code as a workaround though.
Unless your list is sorted, you need to compare every value to the needle. Therefore, both your solution and indexOf will have to execute n/2 comparisons on average. However, since indexOf is a built-in method, it may use additional optimizations and will be slightly faster in practice. Note that unless your application searches in lists extremely often (say a 1000 times per second) or the lists are huge (say 100k entries), the speed difference will not matter.
I benchmarked it multiple times on Google Chrome 52, but feel free to copypaste it into any other browser's console.
~ 1500 ms, includes (~ 2700 ms when I used the polyfill)
var array = [0,1,2,3,4,5,6,7,8,9]; var result = 0; var start = new Date().getTime(); for(var i = 0; i < 10000000; i++) { if(array.includes("test") === true){ result++; } } console.log(new Date().getTime() - start); ~ 1050 ms, indexOf
var array = [0,1,2,3,4,5,6,7,8,9]; var result = 0; var start = new Date().getTime(); for(var i = 0; i < 10000000; i++) { if(array.indexOf("test") > -1){ result++; } } console.log(new Date().getTime() - start); ~ 650 ms, custom function
function inArray(target, array) { /* Caching array.length doesn't increase the performance of the for loop on V8 (and probably on most of other major engines) */ for(var i = 0; i < array.length; i++) { if(array[i] === target) { return true; } } return false; } var array = [0,1,2,3,4,5,6,7,8,9]; var result = 0; var start = new Date().getTime(); for(var i = 0; i < 10000000; i++) { if(inArray("test", array) === true){ result++; } } console.log(new Date().getTime() - start); 1Single line code.. will return true or false
!!(arr.indexOf("val")+1) 4You can use indexOf But not working well in the last version of internet explorer. Code:
function isInArray(value, array) { return array.indexOf(value) > -1; } Execution:
isInArray(1, [1,2,3]); // true I suggest you use the following code:
function inArray(needle, haystack) { var length = haystack.length; for (var i = 0; i < length; i++) { if (haystack[i] == needle) return true; } return false; } Since ECMAScript6, one can use Set :
var myArray = ['A', 'B', 'C']; var mySet = new Set(myArray); var hasB = mySet.has('B'); // true var hasZ = mySet.has('Z'); // false You can use the _contains function from the underscore.js library to achieve this:
if (_.contains(haystack, needle)) { console.log("Needle found."); }; 2In lodash you can use _.includes (which also aliases to _.contains)
You can search the whole array:
_.includes([1, 2, 3], 1); // true You can search the array from a starting index:
_.includes([1, 2, 3], 1, 1); // false (begins search at index 1) Search a string:
_.includes('pebbles', 'eb'); // true (string contains eb) Also works for checking simple arrays of objects:
_.includes({ 'user': 'fred', 'age': 40 }, 'fred'); // true _.includes({ 'user': 'fred', 'age': false }, false); // true One thing to note about the last case is it works for primitives like strings, numbers and booleans but cannot search through arrays or objects
_.includes({ 'user': 'fred', 'age': {} }, {}); // false _.includes({ 'user': [1,2,3], 'age': {} }, 3); // false