Geeks With Blogs
Liam McLennan hackingon.net

This javascript adds a method to javascript arrays that returns a boolean indicating if the supplied object is an element of the array

Array.prototype.contains = function(item) {
    for (var i = 0; i < this.length; i += 1) {
        if (this[i] === item) {
            return true;
        }
    }
    return false;
};

To test

alert([1,1,1,2,2,22,3,4,5,6,7,5,4].contains(2)); // true
alert([1,1,1,2,2,22,3,4,5,6,7,5,4].contains(99)); // false
Posted on Wednesday, March 10, 2010 10:33 PM | Back to top

Copyright © Liam McLennan | Powered by: GeeksWithBlogs.net