Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to find if an item is in a JavaScript array? [duplicate]

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {    for (var i = 0; i < arr.length; i++) {      if (arr[i] == obj) return true;    }  }    console.log(include([1, 2, 3, 4], 3)); // true  console.log(include([1, 2, 3, 4], 6)); // undefined
like image 378
zimbatm Avatar asked Sep 27 '08 15:09

zimbatm


People also ask

How do you check if there are duplicates in an array JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.


2 Answers

As of ECMAScript 2016 you can use includes()

arr.includes(obj); 

If you want to support IE or other older browsers:

function include(arr,obj) {     return (arr.indexOf(obj) != -1); } 

EDIT: This will not work on IE6, 7 or 8 though. The best workaround is to define it yourself if it's not present:

  1. Mozilla's (ECMA-262) version:

       if (!Array.prototype.indexOf)    {          Array.prototype.indexOf = function(searchElement /*, fromIndex */)       {        "use strict";       if (this === void 0 || this === null)        throw new TypeError();       var t = Object(this);      var len = t.length >>> 0;      if (len === 0)        return -1;       var n = 0;      if (arguments.length > 0)      {        n = Number(arguments[1]);        if (n !== n)          n = 0;        else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))          n = (n > 0 || -1) * Math.floor(Math.abs(n));      }       if (n >= len)        return -1;       var k = n >= 0            ? n            : Math.max(len - Math.abs(n), 0);       for (; k < len; k++)      {        if (k in t && t[k] === searchElement)          return k;      }      return -1;    };   } 
  2. Daniel James's version:

     if (!Array.prototype.indexOf) {    Array.prototype.indexOf = function (obj, fromIndex) {      if (fromIndex == null) {          fromIndex = 0;      } else if (fromIndex < 0) {          fromIndex = Math.max(0, this.length + fromIndex);      }      for (var i = fromIndex, j = this.length; i < j; i++) {          if (this[i] === obj)              return i;      }      return -1;    };  } 
  3. roosteronacid's version:

     Array.prototype.hasObject = (    !Array.indexOf ? function (o)    {      var l = this.length + 1;      while (l -= 1)      {          if (this[l - 1] === o)          {              return true;          }      }      return false;    } : function (o)    {      return (this.indexOf(o) !== -1);    }  ); 
like image 78
Vinko Vrsalovic Avatar answered Sep 29 '22 00:09

Vinko Vrsalovic


If you are using jQuery:

$.inArray(5 + 5, [ "8", "9", "10", 10 + "" ]); 

For more information: http://api.jquery.com/jQuery.inArray/

like image 31
Germán Rodríguez Avatar answered Sep 28 '22 23:09

Germán Rodríguez