Array - unique method. - Node.js Array

Node.js examples for Array:Unique Element

Description

Array - unique method.

Demo Code


 /* Returns a new array that is the same as the current one but with all of the duplicates removed.
 * NOTE: Does not work on objects because it uses the == operator and not .equals.
 *///from  w w  w.  j ava2  s. c o m
Array.prototype.unique = function () {
  var r = [];
  o:for(var i = 0, n = this.length; i < n; i++) {
    for(var x = 0, y = r.length; x < y; x++) {
      if(r[x] == this[i]) {
        continue o;
      }
    }
    r[r.length] = this[i];
  }
  return r;
}

Related Tutorials