PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » JavaScript » JavaScript Array Contains

JavaScript Array Contains

Javascript objects are really nice, but sometimes they are missing some useful little functions/methods. The example above is with Arrays. It's really nice to know whether or not an item is contained within your array. Well you can write a function that takes the array and the item you're checking for, but it's much cleaner to add the contains( item ) method to the Array object.

Extending JavaScript Arrays

/**
 * Array.prototype.[method name] allows you to define/overwrite an objects method
 * needle is the item you are searching for
 * this is a special variable that refers to "this" instance of an Array.
 * returns true if needle is in the array, and false otherwise
 */
Array.prototype.contains = function ( needle ) {
   for (i in this) {
       if (this[i] == needle) return true;
   }
   return false;
}

Usage

// Now you can do things like:
var x = Array();
if (x.contains('foo')) {
   // do something special
}

Subscribe to The Thread

  1. I suggest making it more strict,

    if (this[i] == needle) return true;

    to:

    if (this[i] === needle) return true;

    for example, 1 is the same as true. Unless you do ===

  2. Thank you V1, you’re absolutely right. That should definitely be a ===.

  3. “i” should be declared locally (by using a var statement). Currently you’re setting it as a global variable. Also, you shouldn’t be using the “for(i in something)” construct for arrays. Instead use a traditional for or while loop; they’re much quicker.

  4. Here is how I would write it, coming in at a mere 83 kb minified:

    var contains = function( arr, value ) {
    
    	var i = 0, len = arr.length;
    
    	while( i < len && arr[i] != value ) {
    
    		i++;
    
    	}
    
    	return i != len;
    
    };
  5. einstein

    Thank you, It really worked.

  6. Nicer still would be to write it like this:

    function contains(arr, value) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === value) return true;
        }
        return false;
    }
  7. John Doe

    /**
    * is_array
    *
    * @param mixed input
    * @return bol
    */

    function is_array(obj) {
    if (obj.constructor.toString().indexOf(‘Array’) == -1) {
    return false;
    }
    return true;
    }

    /**
    * contains
    *
    * @param mixed input
    * @param string value
    * @return bol
    */

    function contains(input, value) {
    if (!is_array(input)) {
    if (input.indexOf(value) != -1) {
    return true;
    }
    }
    else {
    var i = input.length;
    while (i–) {
    if (input[i] === value) {
    return true;
    }
    }
    }
    return false;
    }

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~