views:

3872

answers:

6

i need to determine if a value exists in an array using javascript.

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

Please suggest what to do

+5  A: 
arrValues.indexOf('Sam') > -1

Apparently IE still doesn't have the Array.prototype.indexOf method. For IE,

if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(needle) {
        for(var i = 0; i < this.length; i++) {
            if(this[i] === needle) {
                return i;
            }
        }
        return -1;
    };
}
eyelidlessness
Note that indexOf on arrays is not implemented in IE, but you can define it yourself
RaYell
you should use a typed comparison with `===` to be compatible with the native implementation
Christoph
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)
Christoph
Thanks for the comments and edits. It's Friday and I've been drinking so I really shouldn't be coding. ;)
eyelidlessness
It's *Saturday* apparently.
eyelidlessness
I have tried all the answers given , but still returning false.
Prasad
Finally its worked. its due to improper trim of the comparing value. there was some space in the comparing value
Prasad
+1  A: 

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

if (arrValues.indexOf('Sam') > -1) {return true;}
else {return false;}
Gabriel Hurley
you can reduce that to: return (arrValues.indexOf('Sam') > -1);
Kenneth J
A: 

The code works in Safari 4.0.2.

BTW: I'd do a === comparison instead of just ==.

Georg
All Javascript implementations I'm aware of necessarily implement an Array object with a prototype. Javascript (the language) is largely built upon itself, with its basic types largely inherited from the Object prototype.
eyelidlessness
I just couldn't think of any other reason why it's not working for him.
Georg
This is rather a comment than an answer.
Gumbo
+1  A: 

The above function always returns false.

No it doesn't: The function works as expected - the error must be somewhere else.

Christoph
+1  A: 

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

Array.prototype.contains = function(obj) {
    return this.indexOf(obj) > -1;
};
rlovtang
That's redundant.
eyelidlessness
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.
rlovtang
+4  A: 

jQuery has a utility function for this:

$.inArray(value, array)

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

codeape