Quantcast
Channel: How do I check if an array includes a value in JavaScript? - Stack Overflow
Browsing all 66 articles
Browse latest View live

Answer by MattMcKnight for How do I check if an array includes a value in...

Thinking out of the box for a second, if you are making this call many many times, it is vastly more efficient to use an associative array a Map to do lookups using a hash function....

View Article


Answer by codeape for How do I check if an array includes a value in JavaScript?

Current browsers have Array#includes, which does exactly that, is widely supported, and has a polyfill for older browsers. > ['joe', 'jane', 'mary'].includes('jane'); true You can also use...

View Article


Answer by Ken for How do I check if an array includes a value in JavaScript?

Here's how Prototype does it: /** * Array#indexOf(item[, offset = 0]) -> Number * - item (?): A value that may or may not be in the array. * - offset (Number): The number of initial items to skip...

View Article

Answer by Mason Houtz for How do I check if an array includes a value in...

Extending the JavaScript Array object is a really bad idea because you introduce new properties (your custom methods) into for-in loops which can break existing scripts. A few years ago the authors of...

View Article

Answer by Már Örlygsson for How do I check if an array includes a value in...

Here's a JavaScript 1.6 compatible implementation of Array.indexOf: if (!Array.indexOf) { Array.indexOf = [].indexOf ? function(arr, obj, from) { return arr.indexOf(obj, from); } : function(arr, obj,...

View Article


Answer by Damir Zekić for How do I check if an array includes a value in...

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...

View Article

Answer by cic for How do I check if an array includes a value in JavaScript?

indexOf maybe, but it's a "JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard." Example: [1, 2, 3].indexOf(1) => 0 ["foo", "bar",...

View Article

Answer by Andru Luvisi for How do I check if an array includes a value in...

If you are using JavaScript 1.6 or later (Firefox 1.5 or later) you can use Array.indexOf. Otherwise, I think you are going to end up with something similar to your original code.

View Article


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++) {...

View Article


Answer by Mamunur Rashid for How do I check if an array includes a value in...

Adding a unique item to a another listsearchResults: [ { name: 'Hello', artist: 'Selana', album: 'Riga', id: 1, }, { name: 'Hello;s', artist: 'Selana G', album: 'Riga1', id: 2, }, { name: 'Hello2',...

View Article

Answer by Majedur Rahaman for How do I check if an array includes a value in...

Object.keys for getting all property names of the object and filter all values that exact or partial match with specified string.function filterByValue(array, string) { return array.filter(o =>...

View Article

Image may be NSFW.
Clik here to view.

Answer by Kamil Kiełczewski for How do I check if an array includes a value...

PerformanceToday 2019.01.07 I perform tests on MacOs HighSierra 10.13.6 on Chrome v78.0.0, Safari v13.0.4 and Firefox v71.0.0 for 15 chosen solutions. Conclusionssolutions based on JSON, Set and...

View Article

Answer by Riwaj Chalise for How do I check if an array includes a value in...

Use indexOf()You can use the indexOf() method to check whether a given value or element exists in an array or not. The indexOf() method returns the index of the element inside the array if it is found,...

View Article


Answer by Md. Harun Or Rashid for How do I check if an array includes a value...

This may be a detailed and easy solution.//plain arrayvar arr = ['a', 'b', 'c'];var check = arr.includes('a');console.log(check); //returns trueif (check){ // value exists in array //write some...

View Article

Answer by da coconut for How do I check if an array includes a value in...

use Array.prototype.includes for example: const fruits = ['coconut', 'banana', 'apple']const doesFruitsHaveCoconut = fruits.includes('coconut')// trueconsole.log(doesFruitsHaveCoconut)maybe read this...

View Article


Answer by Chris Vouga for How do I check if an array includes a value in...

use recursion for the memesconst includes = (x, [h, ...t]) => h && (x === h || includes(x, t))console.log(includes(1, [3,2,0,2,3]))console.log(includes(1, [3,2,1,2,3]))

View Article

Answer by Msilucifer for How do I check if an array includes a value in...

You can use findIndex function to check if an array has a specific value.arrObj.findIndex(obj => obj === comparedValue) !== -1;Returns true if arrObj contains comparedValue, false otherwise.

View Article


Answer by francis for How do I check if an array includes a value in JavaScript?

Using RegExp:console.log(new RegExp('26242').test(['23525', '26242', '25272'].join(''))) // true

View Article

Answer by Ran Turner for How do I check if an array includes a value in...

There are a couple of options for doing that including includes, some, find, findIndex.const array = [1, 2, 3, 4, 5, 6, 7];console.log(array.includes(3));//includes() determines whether an array...

View Article

Image may be NSFW.
Clik here to view.

Answer by Med Aziz CHETOUI for How do I check if an array includes a value in...

The best default method to check if value exist in array JavaScript is some()Array.prototype.some()The some() method tests whether at least one element in the array passes the test implemented by the...

View Article
Browsing all 66 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>