How do you check if an array contains a value in javascript?
A hopefully faster bidirectional Show
2015While the new method It's a long time that I was thinking of a way to replace the slow A performant way has already been
found, looking at the top answers. From those I chose the I also prefer I was curious if the iteration was much slower if I check both sides of the array while doing it. Apparently no, and so this function is around two times faster than the top voted ones. Obviously it's also faster than the native one. This is in a real world environment, where you never know if the value you are searching is at the beginning or at the end of the array. When you know you just pushed an array with a value, using lastIndexOf remains probably the best solution, but if you have to travel through big arrays and the result could be everywhere, this could be a solid solution to make things faster. Bidirectional
Performance testhttps://jsbench.me/7el1b8dj80 As a test I created an array with 100k entries. Three queries: at the beginning, in the middle & at the end of the array. I hope you also find this interesting and test the performance. Note: As you can see I slightly modified the The array prototype variant
The function can also be easily modified to return true or false or even the object, string or whatever it is. And here is the
How is this possible?I think that the simple calculation to get the reflected index in an array is so simple that it's two times faster than doing an actual loop iteration. Here is a complex example doing three checks per iteration, but this is only possible with a longer calculation which causes the slowdown of the code. https://web.archive.org/web/20151019160219/http://jsperf.com/bidirectionalindexof/2 Summary: in this tutorial, you’ll learn how to check if an array contains a value in JavaScript. A value in JavaScript can be primitive such as a number or string. Or it can be an object. This tutorial shows you to check if an array contain a value, being a primtive value or object. To check if an array contains a primitive value, you can use the array method like The following example uses the
If you want to ingore the letter cases when checking, you can:
The following example illustrates the steps:
In this example, the First, the 2) Check if an array contains a numberThe following example shows how to use the
3) Check if an array contains an objectThe following example uses the
In this example, the In practice, instead of searching for a refenrence, you often search for objects by their property values. The following example won’t work:
In this example, the following object:
…looks like the first element in the To check if an array contains an object, you follow these steps:
To compare objects by property values, you use the following helper function:
The And the limitation of the Note that the some library provides the function that allows you compare two objects by their property values. For example, Lodash has a _.isEqual() method that allows you to compare objects if they are equal with a better performance than using the The following uses the
Put it all together:
Summary
Was this tutorial helpful ? How do you check if a value exists in an array?The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.
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 of arrays contains an array JavaScript?Javascript array contains another array
To check if the array contains an array in Javascript, use array some(), and array includes() function. The array some() method checks each element against a test method and returns true if any array item passes the test function. Otherwise, it returns false.
How do you check if an array contains a string?To check if a string is contained in an array, call the indexOf method, passing it the string as a parameter. The indexOf method returns the index of the first occurrence of the string in the array, or -1 if the string is not contained in the array.
|