Eliminate all duplicates from an Array - Node.js Array

Node.js examples for Array:Remove Element

Description

Eliminate all duplicates from an Array

Demo Code


/**//from  ww  w. ja v  a  2  s . com
 * Eliminate all duplicates from an Array
 * http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array
 *
 * @return {Array} original array without duplicates
 *
 */
Array.prototype.removeDuplicates = function() {
    var type = Object.prototype.toString.call(this).split(/\W/)[2];
    if (type === 'Array') {
        return this.reduce(function(accum, cur) {
            if (accum.indexOf(cur) === -1) accum.push(cur);
            return accum;
        }, [] );
    }
};

Related Tutorials