Determine whether an array contains a value [duplicate]
Asked Answered
O

18

1407

I need to determine if a value exists in an array.

I am using the following function:

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

The above function always returns false.

The array values and the function call is as below:

arrValues = ["Sam","Great", "Sample", "High"]
alert(arrValues.contains("Sam"));
Overpraise answered 25/7, 2009 at 8:18 Comment(12)
The code works in Safari 4.0.2. BTW: I'd do a === comparison instead of just ==.Jorrie
"The above function always returns false." No it doesn't: The function works as expected - the error must be somewhere else.Nodus
See also: https://mcmap.net/q/36072/-how-do-i-check-if-an-array-includes-a-value-in-javascript/1569Adele
Finally its worked. its due to improper trim of the comparing value. there was some space in the comparing value (A comment from the asker, to the accepted answer.)Pasta
It works, you should have used === instead of ==Columbic
For whoever is still struggling with it. The following took care of it for me: 1. Install lodash.includes by executing the following command: npm --save lodash.includes 2. Require lodash.includes: var includes = require('lodash.includes'); 3. use it like this: arrayBeingUsed.includes(valueToLookFor); (npmjs.com/package/lodash.includes)Extrude
Related with better answer - https://mcmap.net/q/46286/-check-if-an-element-is-present-in-an-array-duplicateAnecdotist
["Admin", "Action", "WriteType"].find( (key) => { console.log(key = , key); return key === "KeyWord" } ); Scotney
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Scotney
you can use Lodash _.some(arrValues,'Sam') => true /falseSextain
if(numbers.join('') == '') then it means array named 'numbers' is empty.Plowshare
there is no contains method in javascript for array, you can use includes insteadTapdance
F
1013
var contains = function(needle) {
    // Per spec, the way to identify NaN is that it is not equal to itself
    var findNaN = needle !== needle;
    var indexOf;

    if(!findNaN && typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                var item = this[i];

                if((findNaN && item !== item) || item === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle) > -1;
};

You can use it like this:

var myArray = [0,1,2],
    needle = 1,
    index = contains.call(myArray, needle); // true

CodePen validation/usage

Facet answered 25/7, 2009 at 8:22 Comment(24)
Note that indexOf on arrays is not implemented in IE, but you can define it yourselfCourtund
you should use a typed comparison with === to be compatible with the native implementationNodus
fixed the comparison and added the missing return -1; please note that according to the ES5 spec, this method will behave differently from the native one in case ofsigned zeroes and NaNs (see 15.4.4.14 and 9.12 vs. 11.9.6)Nodus
I have tried all the answers given , but still returning false.Overpraise
Finally its worked. its due to improper trim of the comparing value. there was some space in the comparing valueOverpraise
What version of IE does this answer refer to?Andrews
The shim should test if i is a property of this.Biernat
Note that in Sharepoint 2010, WebParts can break if you implement an Array.Prototype: labs.steveottenad.com/type-mismatch-on-wpadder-jsAmbur
@thesystem, that's an odd concern for a method of Array.prototype and an i that is numeric and bound by 0 and this.length. I think we can safely assume that i is a property of this, given those characteristics. I suppose it could be an issue if you call Array.prototype.indexOf.call(someArbitraryObjectWithALengthProperty), but that would be true of the native method as well.Facet
@wilsjd, I have been meaning to update this for some time, to warn against patching prototypes.Facet
var i = -1, index; Can anyone explain this part? (it returns undefined if the needle is not found in the array) testing on IE8, works fine for firefoxSorrells
How about this: ~array.indexOf(value) -- It'll be truthy if the value exists and falsy otherwhise. So you could do something like: if (~array.indexOf(value)) { console.log('Found it!'); }Caterinacatering
@gtramontina, that is very "clever" but confusing to anyone who doesn't know what it does. In order to make it maintainable, you'd overcome your savings in code characters by documenting it with a comment.Facet
@Facet I'd rather document it with tests.Caterinacatering
@gtramontina, but if anyone else has to read your code, it will be confusing. If not, do whatever you want... but then that calls into question advocating it on a Q&A site.Facet
@Caterinacatering Why would you do a bitwise not (~) rather than a logical not (!)? I guess in this case the end result is the same, but as the next person reading your code, I would spend a few minutes puzzling on that one, as I just did.Degrade
A logical not (!) on its own – i.e. if (!array.indexOf(value)) – would fail the case where the element exists on the first position (0), as 0 is falsy.Caterinacatering
Doesn't work for NaNIl
@threed, that's a good catch, but I'm a little stumped about how best to fix it for SO users. I briefly edited the non-native implementation to handle NaN, then checked it in CodePen; I realized that the native Array.prototype.indexOf method (at least in Chrome) also doesn't support NaN. So on the one hand, the behavior is technically incorrect, but on the other hand it has parity with what the browser provides. I know how I'd solve it in my own projects (a wrapper function, completely sidestepping the native implementation for NaN), but I'm cautious about misdirecting SO users.Facet
@threed, thinking more on this, I realize the actual question is not about indexOf, that was just an implementation detail. I'll more thoroughly update the answer.Facet
Good call. I recommend taking a look at the MDN polyfill for the Array.includes method.Il
@threed, I took a look at the MDN page, and I honestly found it hard to read. That said, I've updated my answer to handle NaN (and included the CodePen)Facet
var numbers = [0,1,2,3,4,5,6,7,8,9]; return jQuery.inArray( parseInt(e.key), numbers )!=-1;Trotman
[1, 2, 3].includes(2); Array.includes(val) is available since year 2016Admire
V
1021

jQuery has a utility function for this:

$.inArray(value, array)

Returns index of value in array. Returns -1 if array does not contain value.

See also How do I check if an array includes an object in JavaScript?

Vd answered 24/9, 2009 at 19:34 Comment(9)
Don't let the name "inArray" fool you. As mentioned above (but of course I didn't read closely enough), returns -1 (not false) if the element doesn't exist.Meso
@Steve Paul what is wrong with name? it does what it says: -1=it's NOT there, anything else= it's thereCherlynchernow
'inArray' implies that a boolean will be returned indicating whether the element could be found in the array. Therefore users may feel tempted to use the expression: if ($.inArray('myval', myarray)) {...} This will evaluate to true if 'myval' is NOT in myarray. Furthermore, it will evaluate to false if myval's index is 0.Gobi
The non-booleanness of $.inArray's return definitely feels like a mistake on jQuery's part. Surely, it ought to be renamed to $.indexOf, if that's the function it's polyfilling?Nightclub
Works, but requires the jQuery framework. Not ideal if you're not using it otherwise.Menology
It's very easy to suggest that someone uses a JQuery method for everything javascript, but as the original question was for a javascript solution, then one should be provided in that mannerImpromptu
Does not work for NaNIl
Also worth mentioning that $.inArray('0', ['0', '1', '2']) will return 0, which should be true, but inside and if it evaluates to false.Infantry
You could use a bitwise operator like if (!~$.inArray(0, [13, 27, 35, 36, 37, 38, 39, 40])) {}. The tilde (~) transforms -1 to 0 and 0 is equal to false in Javascript.Dorothadorothea
F
1013
var contains = function(needle) {
    // Per spec, the way to identify NaN is that it is not equal to itself
    var findNaN = needle !== needle;
    var indexOf;

    if(!findNaN && typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                var item = this[i];

                if((findNaN && item !== item) || item === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle) > -1;
};

You can use it like this:

var myArray = [0,1,2],
    needle = 1,
    index = contains.call(myArray, needle); // true

CodePen validation/usage

Facet answered 25/7, 2009 at 8:22 Comment(24)
Note that indexOf on arrays is not implemented in IE, but you can define it yourselfCourtund
you should use a typed comparison with === to be compatible with the native implementationNodus
fixed the comparison and added the missing return -1; please note that according to the ES5 spec, this method will behave differently from the native one in case ofsigned zeroes and NaNs (see 15.4.4.14 and 9.12 vs. 11.9.6)Nodus
I have tried all the answers given , but still returning false.Overpraise
Finally its worked. its due to improper trim of the comparing value. there was some space in the comparing valueOverpraise
What version of IE does this answer refer to?Andrews
The shim should test if i is a property of this.Biernat
Note that in Sharepoint 2010, WebParts can break if you implement an Array.Prototype: labs.steveottenad.com/type-mismatch-on-wpadder-jsAmbur
@thesystem, that's an odd concern for a method of Array.prototype and an i that is numeric and bound by 0 and this.length. I think we can safely assume that i is a property of this, given those characteristics. I suppose it could be an issue if you call Array.prototype.indexOf.call(someArbitraryObjectWithALengthProperty), but that would be true of the native method as well.Facet
@wilsjd, I have been meaning to update this for some time, to warn against patching prototypes.Facet
var i = -1, index; Can anyone explain this part? (it returns undefined if the needle is not found in the array) testing on IE8, works fine for firefoxSorrells
How about this: ~array.indexOf(value) -- It'll be truthy if the value exists and falsy otherwhise. So you could do something like: if (~array.indexOf(value)) { console.log('Found it!'); }Caterinacatering
@gtramontina, that is very "clever" but confusing to anyone who doesn't know what it does. In order to make it maintainable, you'd overcome your savings in code characters by documenting it with a comment.Facet
@Facet I'd rather document it with tests.Caterinacatering
@gtramontina, but if anyone else has to read your code, it will be confusing. If not, do whatever you want... but then that calls into question advocating it on a Q&A site.Facet
@Caterinacatering Why would you do a bitwise not (~) rather than a logical not (!)? I guess in this case the end result is the same, but as the next person reading your code, I would spend a few minutes puzzling on that one, as I just did.Degrade
A logical not (!) on its own – i.e. if (!array.indexOf(value)) – would fail the case where the element exists on the first position (0), as 0 is falsy.Caterinacatering
Doesn't work for NaNIl
@threed, that's a good catch, but I'm a little stumped about how best to fix it for SO users. I briefly edited the non-native implementation to handle NaN, then checked it in CodePen; I realized that the native Array.prototype.indexOf method (at least in Chrome) also doesn't support NaN. So on the one hand, the behavior is technically incorrect, but on the other hand it has parity with what the browser provides. I know how I'd solve it in my own projects (a wrapper function, completely sidestepping the native implementation for NaN), but I'm cautious about misdirecting SO users.Facet
@threed, thinking more on this, I realize the actual question is not about indexOf, that was just an implementation detail. I'll more thoroughly update the answer.Facet
Good call. I recommend taking a look at the MDN polyfill for the Array.includes method.Il
@threed, I took a look at the MDN page, and I honestly found it hard to read. That said, I've updated my answer to handle NaN (and included the CodePen)Facet
var numbers = [0,1,2,3,4,5,6,7,8,9]; return jQuery.inArray( parseInt(e.key), numbers )!=-1;Trotman
[1, 2, 3].includes(2); Array.includes(val) is available since year 2016Admire
L
928

This is generally what the indexOf() method is for. You would say:

return arrValues.indexOf('Sam') > -1
Lighter answered 25/7, 2009 at 8:25 Comment(15)
I really like that you can do this which is essentially the same thing as the jQuery answer above, but without jQuery - plus you can simplify it as Kenneth pointed out, although I couldn't make that work in the console.Nanci
indexOf does not work in <IE9.Rockwell
Does not work with NaNIl
I like how Kenneth J's comment has more upvotes than the answer. But good stuff - I've used indexOf() for strings, but I didn't know you could use it for arrays in general.Pathos
return ~arrValues.indexOf('Sam') will return false if element does not exist in arrayTank
IE support can matter if you're targeting certain countries where users use dilapidated hardware.Extract
@SujayPhadke: Because money. sitepoint.com/browser-trends-january-2015-ie8-usage-triples . Depending on niche and target market, the percentage may vary.Kopeck
@SebastianMach and if all web devs just took a stand saying no... users will be forced to change. and helps companies save money since they don't have to hire IE developersOutshout
@SujayPhadke: Unfortunately, that's an Utopia. For any group of people beyond a critical mass, there will never be perfect agreement.Kopeck
@SebastianMach agreed. But it has happened before, (recent past). Adobe Flash had a huge critical mass, no one can argue that. It took one company to make a logical tech argument to end flash on the web and bring in standards.Outshout
@SujayPhadke: True. It was the company holding the de facto monopoly. Microsoft would be required to destroy IE. However, would they deploy such a mandatory uninstallation, there would be users outcrying (at least I bet so).Kopeck
@SujayPhadke Or the developers who make a stand will get fired and the company will hire new devs who can comply with IE. It goes both ways.Hy
"Doesn't work on <IE9" - users shall upgrade their browsers, it's not that hard. Even Microsoft buried their IE browser, because it was so bad (and nobody really liked it).Haemolysin
@DougS indexOf works in node.js, so awesome!Landin
Works fine for latest browsers and also string literals. Superb!Plagal
C
427

Array.prototype.includes()

In ES2016, there is Array.prototype.includes().

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

Example

["Sam", "Great", "Sample", "High"].includes("Sam"); // true

Support

According to kangax and MDN, the following platforms are supported:

  • Chrome 47
  • Edge 14
  • Firefox 43
  • Opera 34
  • Safari 9
  • Node 6

Support can be expanded using Babel (using babel-polyfill) or core-js. MDN also provides a polyfill:

if (![].includes) {
  Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
    'use strict';
    var O = Object(this);
    var len = parseInt(O.length) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1]) || 0;
    var k;
    if (n >= 0) {
      k = n;
    } else {
      k = len + n;
      if (k < 0) {k = 0;}
    }
    var currentElement;
    while (k < len) {
      currentElement = O[k];
      if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) {
        return true;
      }
      k++;
    }
    return false;
  };
}
Choragus answered 9/6, 2015 at 6:45 Comment(2)
I'm tempted to post this answer across several javascript questions concerning multiple variable values or arrays. The polyfill from MDN is nice.Chavaree
thanks for this! Babel polyfils it nicely :-) I remember 2 years ago jQuery was in any project and nowadays it's Babel :) big win for JavaScript community! There's table of what's available of ES7 already on many platforms including babel pollyfils kangax.github.io/compat-table/es2016plusAppliance
V
144

It's almost always safer to use a library like lodash simply because of all the issues with cross-browser compatibilities and efficiency.

Efficiency because you can be guaranteed that at any given time, a hugely popular library like underscore will have the most efficient method of accomplishing a utility function like this.

_.includes([1, 2, 3], 3); // returns true

If you're concerned about the bulk that's being added to your application by including the whole library, know that you can include functionality separately:

var includes = require('lodash/collections/includes');

NOTICE: With older versions of lodash, this was _.contains() rather than _.includes().

Venial answered 25/1, 2014 at 3:0 Comment(7)
@threed, you don't have to include the whole library. Partial functionality can be included with require('lodash/collections/contains').Venial
FYI: With lodash 4 it's now _.includes() instead of _.contains().Philharmonic
helper libraries may seem convenient but tend to change (too) often, see comment above "lodash 4 '.contains()' is now '.includes()'"Hast
@Hast that could be said about any library. that's why you have semver and dependency management.Venial
In Lodash versions prior to 2.x _.include() is an alias for _.contains() and there is no _.includes() defined. As of ver. 3.x, _.include() and _.contains() are both aliases for _includes(). But as of ver. 4.x, there is only _.includes() and no more other aliases.Fetal
I once wanted to retrieve values from an object via a case-insensitive index look up. lodash doesn't (didn't at the time at least) have a performant way of doing this. So you can't just assume that because it's a popular library that it will always be performant for your specific requirementCollado
@ZachSmith I understand. But emphasis on the "specific requirement" there. I only mentioned this as an "almost always" applicable answer. And case-insensitive index lookups are the exception to the norm - hence, lodash isn't optimised for it.Venial
M
54

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
Mufi answered 7/1, 2016 at 13:5 Comment(1)
nice way to use actually set.Belligerency
I
47

tl;dr

function includes(k) {
  for(var i=0; i < this.length; i++){
    if( this[i] === k || ( this[i] !== this[i] && k !== k ) ){
      return true;
    }
  }
  return false;
}

Example

function includes(k) {
  for(var i=0; i < this.length; i++){
    if( this[i] === k || ( this[i] !== this[i] && k !== k ) ){
      return true;
    }
  }
  return false;
}

function log(msg){
  $('#out').append('<div>' + msg + '</div>');  
}

var arr = [1, "2", NaN, true];
arr.includes = includes;

log('var arr = [1, "2", NaN, true];');
log('<br/>');
log('arr.includes(1): ' + arr.includes(1));
log('arr.includes(2): ' + arr.includes(2));
log('arr.includes("2"): ' + arr.includes("2"));
log('arr.includes(NaN): ' + arr.includes(NaN));
log('arr.includes(true): ' + arr.includes(true));
log('arr.includes(false): ' + arr.includes(false));
#out{
  font-family:monospace;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=out></div>

Longer Answer

I know this question isn't really about whether or not to extend built-in objects, but the attempt of the OP and the comments on this answer highlight that debate. My comment from Feb 12, '13 cites an article that outlines this debate really well, however that link broke and I can't edit the original comment because too much time has passed, so I include it here.

If you're looking to extend the built-in Array object with a contains method, probably the best and most responsible way to do this would be to use this polyfill from MDN. (See also this section of the MDN article on Prototypical inheritance, which explains that "The only good reason for extending a built-in prototype is to backport the features of newer JavaScript engines; for example Array.forEach, etc.")

if (!Array.prototype.includes) {
  Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
    'use strict';
    var O = Object(this);
    var len = parseInt(O.length) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1]) || 0;
    var k;
    if (n >= 0) {
      k = n;
    } else {
      k = len + n;
      if (k < 0) {k = 0;}
    }
    var currentElement;
    while (k < len) {
      currentElement = O[k];
      if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) {
        return true;
      }
      k++;
    }
    return false;
  };
}

Don't want strict equality, or want to choose?

function includes(k, strict) {
  strict = strict !== false; // default is true
  // strict = !!strict; // default is false
  for(var i=0; i < this.length; i++){
    if( (this[i] === k && strict) || 
        (this[i] == k && !strict) ||
        (this[i] !== this[i] && k !== k)
    ) {
      return true;
    }
  }
  return false;
}
Il answered 15/9, 2011 at 20:3 Comment(4)
Isn't this technique of augmenting built-in types frowned upon?Prestissimo
Buggy: [1,2,4].contains([].contains) is true. Also unnecessarily slow due to the same bug. Avoid for..in over arrays.Ecclesiasticism
@Eamon Nerbonne: I just pasted that code into jsfiddle.net and got false. Am I doing something wrong. Also, could you elaborate on how this bug slows the code down? Finally, I wasn't aware that there is a performance difference for "for..in" loops. Could you explain or direct me towards an article where I could read more?Il
@threed totally untrue: "This is a very common practice and is used extensively by jQuery" - they create new prototypes for some objects they use, but NOT modify built-in prototypesKarolkarola
M
22

My little contribution:

function isInArray(array, search)
{
    return array.indexOf(search) >= 0;
}

//usage
if(isInArray(my_array, "my_value"))
{
    //...
}
Marianelamariani answered 13/9, 2013 at 17:29 Comment(0)
B
20

If you have access to ECMA 5 you can use the some method.

MDN SOME Method Link

arrValues = ["Sam","Great", "Sample", "High"];

function namePresent(name){
  return name === this.toString();
}
// Note:
// namePresent requires .toString() method to coerce primitive value
// i.e. String {0: "S", 1: "a", 2: "m", length: 3, [[PrimitiveValue]]: "Sam"}
// into
// "Sam"

arrValues.some(namePresent, 'Sam');
=> true;

If you have access to ECMA 6 you can use the includes method.

MDN INCLUDES Method Link

arrValues = ["Sam","Great", "Sample", "High"];

arrValues.includes('Sam');
=> true;
Burbank answered 31/7, 2015 at 15:46 Comment(0)
T
18

Given the implementation of indexOf for IE (as described by eyelidlessness):

Array.prototype.contains = function(obj) {
    return this.indexOf(obj) > -1;
};
Transfiguration answered 25/7, 2009 at 9:12 Comment(3)
That's redundant.Facet
Maybe, but it makes your code cleaner. if (myArray.contains(obj)) is easier to read and states the intent better than if (myArray.indexOf(obj) > -1). I definitively would implement both.Transfiguration
Does this work in all browsers? Or do some browsers consider the index of "2" and 2 the same?Il
H
12

You can use _.indexOf method or if you don't want to include whole Underscore.js library in your app, you can have a look how they did it and extract necessary code.

    _.indexOf = function(array, item, isSorted) {
    if (array == null) return -1;
    var i = 0, l = array.length;
    if (isSorted) {
      if (typeof isSorted == 'number') {
        i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted);
      } else {
        i = _.sortedIndex(array, item);
        return array[i] === item ? i : -1;
      }
    }
    if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
    for (; i < l; i++) if (array[i] === item) return i;
    return -1;
  };
Heffner answered 11/1, 2013 at 16:54 Comment(0)
G
10

Another option would be to use Array.some (if available) in the following way:

Array.prototype.contains = function(obj) {
  return this.some( function(e){ return e === obj } );
}

The anonymous function passed to Array.some will return true if and only if there is an element in the array that is identical to obj. Absent such an element, the function will not return true for any of the elements of the array, so Array.some will return false as well.

Godderd answered 8/2, 2015 at 0:37 Comment(0)
I
8

Wow, there are a lot of great answers to this question.

I didn't see one that takes a reduce approach so I'll add it in:

var searchForValue = 'pig';

var valueIsInArray = ['horse', 'cat', 'dog'].reduce(function(previous, current){
    return previous || searchForValue === current ? true : false;
}, false);

console.log('The value "' + searchForValue + '" is in the array: ' + valueIsInArray);

Here's a fiddle of it in action.

Infract answered 7/3, 2016 at 15:51 Comment(2)
I was actually thinking about going this route, but it still has to transverse the entire array even when a result is found.Erenow
@Erenow that's a good point. It's def something to keep in mind when picking which method to use. You could potentially wrap the reduce statement in a try catch block and throw an exception once you've found the value, but if you do that I think you lose some of the simplicity that the functional approach gives you.Infract
A
6

The answer provided didn't work for me, but it gave me an idea:

Array.prototype.contains = function(obj)
    {
        return (this.join(',')).indexOf(obj) > -1;
    }

It isn't perfect because items that are the same beyond the groupings could end up matching. Such as my example

var c=[];
var d=[];
function a()
{
    var e = '1';
    var f = '2';
    c[0] = ['1','1'];
    c[1] = ['2','2'];
    c[2] = ['3','3'];
    d[0] = [document.getElementById('g').value,document.getElementById('h').value];

    document.getElementById('i').value = c.join(',');
    document.getElementById('j').value = d.join(',');
    document.getElementById('b').value = c.contains(d);
}

When I call this function with the 'g' and 'h' fields containing 1 and 2 respectively, it still finds it because the resulting string from the join is: 1,1,2,2,3,3

Since it is doubtful in my situation that I will come across this type of situation, I'm using this. I thought I would share incase someone else couldn't make the chosen answer work either.

Aleen answered 24/5, 2011 at 18:9 Comment(1)
This solution seems very fragile and prone to error in all but the most narrow cases. Imagine, for example, using this array: var arr = ["Smith, Reed", "Jackson, Frank"]; arr.contains(searchTerm); Image that some user accidentally typed "Reed,Jackson" instead of "Reed, Jackson" in some text field that searched through this array. This algorithm would return a false positive and the user would think that Reed, Jackson actually existed when it doesn't. Cases like this are why this algorithm is much more prone to bugs.Il
S
5

Using array .map function that executes a function for every value in an array seems cleanest to me.

Ref: Array.prototype.map()

This method can work well both for simple arrays and for arrays of objects where you need to see if a key/value exists in an array of objects.

function inArray(myArray,myValue){
    var inArray = false;
    myArray.map(function(key){
        if (key === myValue){
            inArray=true;
        }
    });
    return inArray;
};

var anArray = [2,4,6,8]
console.log(inArray(anArray, 8)); // returns true
console.log(inArray(anArray, 1)); // returns false

function inArrayOfObjects(myArray,myValue,objElement){
    var inArray = false;
    myArray.map(function(arrayObj){
        if (arrayObj[objElement] === myValue) {
            inArray=true;
        }
    });
    return inArray;
};

var objArray = [{id:4,value:'foo'},{id:5,value:'bar'}]
console.log(inArrayOfObjects(objArray, 4, 'id')); // returns true
console.log(inArrayOfObjects(objArray, 'bar', 'value')); // returns true
console.log(inArrayOfObjects(objArray, 1, 'id')); // returns false
Swart answered 16/6, 2015 at 17:6 Comment(0)
S
2
function setFound(){   
 var l = arr.length, textBox1 = document.getElementById("text1");
    for(var i=0; i<l;i++)
    {
     if(arr[i]==searchele){
      textBox1 .value = "Found";
      return;
     }
    }
    textBox1 .value = "Not Found";
return;
}

This program checks whether the given element is found or not. Id text1 represents id of textbox and searchele represents element to be searched (got fron user); if you want index, use i value

Showily answered 24/7, 2014 at 8:40 Comment(3)
Please give the explanation to your code. Code only answers are not appreciatedPurdah
This program checks whether the given element is found or not. Id text1 represents id of textbox and searchele represents element to be searched (got fron user); if you want index, use i value.Showily
Please update it in your answer.Purdah
H
1

The simplest solution for a contains function, would be a function that looks like this :

var contains = function (haystack, needle) {
    return !!~haystack.indexOf(needle);
}

Ideally, you wouldn't make this a stand-alone function, though, but part of a helper library :

var helper = {};

helper.array = {
    contains : function (haystack, needle) {
        return !!~haystack.indexOf(needle);
    }, 
    ...
};

Now, if you happen to be one of those unlucky people who still needs to support IE<9 and thus can't rely on indexOf, you could use this polyfill, which I got from the MDN :

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {
    var k;
    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }
    var o = Object(this);
    var len = o.length >>> 0;
    if (len === 0) {
      return -1;
    }
    var n = +fromIndex || 0;

    if (Math.abs(n) === Infinity) {
      n = 0;
    }
    if (n >= len) {
      return -1;
    }
    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
    while (k < len) {
      if (k in o && o[k] === searchElement) {
        return k;
      }
      k++;
    }
    return -1;
  };
}
Hallock answered 26/2, 2016 at 0:10 Comment(0)
R
-7

I prefer simplicity:

var days = [1, 2, 3, 4, 5];
if ( 2 in days ) {console.log('weekday');}
Rayerayfield answered 6/5, 2014 at 7:2 Comment(1)
He wants to find a value in an array, not an index. So this won't do the job, even if it is simple. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Bronchopneumonia

© 2022 - 2024 — McMap. All rights reserved.