ChatGPT解决这个技术问题 Extra ChatGPT

Check if an element is present in an array [duplicate]

This question already has answers here: How do I check if an array includes a value in JavaScript? (57 answers) Closed 5 years ago.

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.

Looks fine to me. Of course, if your array is sorted you could do a binary-search instead. Or if each value in the array is always unique you could use a map-based approach instead.
The == operator? Do you really want to explicitly allow type coercion? Of course not. Therefore, use the === operator instead.
It's smart to declare count before the loop. You could also replace those two lines with just for(var i=haystack.length; i--;)
For numbers one can also use the in operator (e.g. (5 in array). It will probably be faster than other options, but won't work for string or objects or any other non-number.

A
Alister

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.


What's a polyfill?
@nirvanaswap A polyfill is a script you can use to ensure that any browser will have an implementation of something you're using. In this case, you'd add a script that checks if ("includes" in Array.prototype) and if not, implements it (using a solution like Benny's answer, for instance). The MDN docs (also linked to in this answer) actually provide one for you.
i used this like below, and working fine. And don't push duplicate value. Thanks a lot!! this.state.UpdatedfriendList.includes(s.valueKey)===false? this.state.UpdatedfriendList.push(s.valueKey):'';
@PrithiviRaj It would be linear time, so performance impact would be directly proportional to the size of the array.
Using widely supported myArray.indexOf(myVal) > -1 is same thing but safer
g
gouessej

Code:

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); // true


!==-1 [extra chars]
indexOf not working carefully in IE browser.
@totaldesign not working carefully, do you mean it is working carelessly in IE? :D
works in node 4.4.7
To my knowledge, ES2016 (which includes the includes method for Arrays) is the seventh edition of ECMAScript. The 6. edition is ES 2015 and does not contain "includes". Please correct. en.wikipedia.org/wiki/ECMAScript#7th_Edition_-_ECMAScript_2016
C
Community

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


Array.prototype.indexOf isn't implemented in IE8.
Not supported in IE (maybe only in 9) stackoverflow.com/questions/1744310/…
This is noted on the page, but it's worth mentioning as part of the answer: indexOf is a relatively new addition to JavaScript, and is not supported in IE version prior to 9.0. Also worth noting that indexOf will still be O(n), so if the OP meant "better" in terms of speed/performance this won't really be any better, just shorter.
@Tomalak Geret'kal True, although the argument is very easy. Added a paragraph about performance, just in case that performance is what the OP meant with better.
@Francisc - Then you might try a map-based approach. Then your inArray() implementation could be as simple as return haystack[needle] != undefined;.
p
psyduck.the.apex.predator

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);

~ 950 / 750 / 650 on my laptop. I just changed array to ['df','ff',2,3,4,5,6,333,8,9] and got ~ 950 / 900 / 3150
A
Akarsh Satija

Single line code.. will return true or false

!!(arr.indexOf("val")+1)

bc everyone loves tilde !!~arr.indexOf("val")
or just use ~arr.indexOf("val"). 0 = falsey, other numbers = truthy
uhhh, 0 should be true because it is found as the first element in the array?
Best solution if you have to cater towards IE11.
E
Eem Jee

You 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;
}

N
Nicolas

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

C
Chris Alley

You can use the _contains function from the underscore.js library to achieve this:

if (_.contains(haystack, needle)) {
  console.log("Needle found.");
};

underscore library just for this?
Not necessarily, as the developer may already be using Underscore in their app for other features. Using Underscore is one solution among many.
C
Connor Leech

In 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