Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

Array.prototype.unique = function()
 {
    this.sort();//from  w w w.  j a  v  a 2s  .co  m
    var re=[this[0]];
    for(var i = 1; i < this.length; i++)
    {
        if( this[i] !== re[re.length-1])
        {
            re.push(this[i]);
        }
    }
    return re;
 }
 
 //??
 Array.prototype.union = function(a)
 {
   return this.concat(a).unique(); 
 
 }

 Array.prototype.minus = function(a)
 { 
    var result =[];
    var clone = this;
      for(var i=0; i < clone.length; i++)
      {
          var flag = true; 
          for(var j=0; j < a.length; j++)
          {   
            if(clone[i] == a[j])   
            flag = false;   
          }   
        if(flag)   
        result.push(clone[i]); 
 
      }
 
    return result.unique(); 
 
 }

Array.prototype.intersect = function(b) { 
    var result = [];
    var a = this;
    for(var i = 0; i < b.length; i ++) {
        var temp = b[i];
        for(var j = 0; j < a.length; j ++) {
            if(temp === a[j]) {
                result.push(temp);
                break;
            }
        }
    }
    return result.unique();
}

Related

  1. unique()
    Array.prototype.unique = function () {
      var tmp = {}, result = [];
      for (var i = 0; i < this.length; i++) {
        if (!tmp[this[i]]) {
          result.push(this[i]);
          tmp[this[i]] = true;
      return result;
    ...
    
  2. unique()
    var fs = require('fs');
    Array.prototype.unique = function() {
      var res = [],
        hash = {};
      for (var i = 0, elem;
        (elem = this[i]) != null; i++) {
        if (!hash[elem]) {
          res.push(elem);
          hash[elem] = true;
    ...
    
  3. unique()
    Array.prototype.unique = function() {
        var newArr = [],
            origLen = this.length,
            found, x, y;
        for (x = 0; x < origLen; x++) {
            found = undefined;
            for (y = 0; y < newArr.length; y++) {
                if (this[x] === newArr[y]) {
                    found = true;
    ...
    
  4. unique()
    Array.prototype.unique = function() {
        return this.reduce(function(accum, current) {
            if (accum.indexOf(current) < 0) {
                accum.push(current);
            return accum;
        }, []);
    };
    function mutation(arr) {
    ...
    
  5. unique()
    Array.prototype.unique = function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
        return a;
    ...
    
  6. unique()
    Array.prototype.unique = function() {
      var n = {},r=[];
      for(var i = 0; i < this.length; i++) {
        if ( this[i] && !n[this[i]]) {
          n[this[i]] = true;
          r.push(this[i]);
      return r;
    ...
    
  7. unique()
    Array.prototype.unique = function () {
      var obj,
        result = [];
      for (var i = 0, l = this.length; i < l; ++i) {
        obj = this[ i ];
        if ( result.indexOf( obj ) < 0 ) {
          result[ result.length ] = obj;
      return result;
    };
    
  8. unique()
    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];
    ...
    
  9. unique()
    Array.prototype.unique = function(){
        var i,j,len = this.length,tempArr = [],tempLen,state;
        for(i = 0 ; i < len ; i++){
            tempLen = tempArr.length;
            state = true;
            for(j = 0 ; j<tempLen;j++){
                if(this[i] == tempArr[j]){
                    state = false;
            if(state){
                tempArr.push(this[i]);
        return tempArr;
    };