0

Essentially I require:

if( [1,2].contains(2) ) {...}

I am surprised that I can't find a simple solution to this.

Is it possible to add a contains method to JavaScript's List type?

Underscore's similar functions: _.contains vs. _.some and _.map vs _.each offers:

_.contains([1,2], 2);

However, _ is JQuery, and it is still rather clumsy.

Community
  • 1
  • 1
P i
  • 29,020
  • 36
  • 159
  • 267
  • Everybody is posting the simple `indexOf` method for arrays - here are the docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – somethinghere Jul 08 '15 at 11:12
  • Nobody has yet hit the nail on head -- is there any way to use exactly the syntax I specified? – P i Jul 08 '15 at 11:18
  • 1
    So you want the function to return a `boolean`? You could simply do this then (if you insist): `Array.prototype.contains(i){ return this.indexOf(i) >= 0; }` and call it with `[1,2].contains(1)`. But thats just an extra work around for an answer you can essentially get quicker without wrapping it. (Edit: oh shit theres an answer saying that already) – somethinghere Jul 08 '15 at 11:23

6 Answers6

4

You can take advantage of indexOf.

Array.prototype.contains = function(el) {
    return this.indexOf(el) > -1;
};

if([1, 2].contains(1)) { // Usage
Tushar
  • 85,780
  • 21
  • 159
  • 179
3

The solution of Tushar is right, but I would recomend do it this way:

Object.defineProperty(Array.prototype, 'contains', {
    writable: true,
    configurable: true,
    enumerable: false,
    value: function(val) {
        return this.indexOf(val) !== -1;
    }
};

The reason for that is that many libraries depend on the construction

for (var i in array) ...

If you define your function without setting enumerable property to false, then all such cycles would eterate through your function as well. Enumerable = false prevents that.

Maxim Gritsenko
  • 2,396
  • 11
  • 25
  • Thats actually a nice thing to mention. I rarely use this and I don;t know how supported it is today but it is certainly good to know. – somethinghere Jul 08 '15 at 11:25
2

You can use indexOf:

if([1,2].indexOf(2) != -1){
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

Modern browsers have Array#indexOf, which does exactly that; this is in the new(ish) ECMAScript 5th edition specification, but it has been in several browsers for years. Older browsers can be supported using the code listed in the "compatibility" section at the bottom of that page.

jQuery has a utility function for this:

$.inArray(value, array)

It returns the index of a value in an array. It returns -1 if the array does not contain the value.

jQuery has several useful utility functions.

An excellent JavaScript utility library is underscore.js:

Some other frameworks:

Notice that some frameworks implement this as a function, while others add the function to the array prototype.


SOURCE

Community
  • 1
  • 1
greenhoorn
  • 1,601
  • 2
  • 15
  • 39
1

if ( [1, 2].indexOf(2) != -1 ) {
  alert("2 is contained in [1, 2]"); 
}
else {
  alert("2 is not contained in [1, 2] (which is paradoxal...)"); 
}

Here is a simple way to check it with array.indexOf().

Anwar
  • 4,162
  • 4
  • 41
  • 62
0

Try the JQuery $.inArray(array,vale) function

$.inArray([1,2], 2)

It returns the index of a value in an array. It returns -1 if the array does not contain the value.

Sapikelio
  • 2,594
  • 2
  • 16
  • 40