Remove an object from an array. Iterates through the array to find the item, then removes it. - Node.js Array

Node.js examples for Array:Remove Element

Description

Remove an object from an array. Iterates through the array to find the item, then removes it.

Demo Code

/** /*from  w  w w.j a  v a  2 s . c o  m*/
 * Function: removeItem
 * Remove an object from an array. Iterates through the array
 *     to find the item, then removes it.
 *
 * Parameters:
 * array - {Array}
 * item - {Object}
 * 
 * Return
 * {Array} A reference to the array
 */
removeItem = function(array, item) {
    for(var i = array.length - 1; i >= 0; i--) {
        if(array[i] == item) {
            array.splice(i,1);
            //break;more than once??
        }
    }
    return array;
};

Related Tutorials