Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

Array.prototype.unique = function() {
   var arr = [this[0]];

   for (var i = 1; i < this.length; i++) {
      if ( arr.indexOf (this[i]) === -1) {
         arr.push(this[i]);//from   w w w  .j  a  v  a  2s .co m
      }
   }
   return arr;
}


var arr = [1, 2, 1, 3, 5, 3, 5, '5'];
console.log(arr.unique());

console.time();
var unique2 = function(arr) {
   var 
      ary = [],
      i = 0,
      key;

   for (; key = arr[i++]; ) {
      if ( ary.indexOf(key) === -1 ) {
         ary.push(key);
      }
   }

   return ary;
}

console.log(unique2(arr));
console.timeEnd();

Related

  1. unique()
    Array.prototype.unique = function(){
      var n=[];
      this.forEach(function(v){if(!n.inArray(v)){n.push(v);}});
      return n;
    };
    
  2. unique()
    var nums =[{name: 1, val: 1},
    {name: 2, val: 1},
    {name: 3, val: 2},
    {name: 4, val: 2}];
    Array.prototype.unique = function() {
        return this.reduce(function(accum, current) {
            if (accum.indexOf(current) < 0) {
                accum.push(current);
            return accum;
        }, []);
    var newArr = [];
    for (var i = 0; i < nums.length; i++) {
      newArr.push(nums[i].val);
    };
    console.log(newArr.unique());
    
  3. unique()
    Array.prototype.unique = function(){
      'use strict';
      var im = {}, uniq = [];
      for (var i=0;i<this.length;i++){
        var type = (this[i]).constructor.name, 
            val = type + (!/num|str|regex|bool/i.test(type) 
                   ? JSON.stringify(this[i]) 
                   : this[i]);
        if (!(val in im)){uniq.push(this[i]);}
    ...
    
  4. unique()
    Array.prototype.unique = function () {
        var r = new Array();
        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;
    ...
    
  5. unique()
    Array.prototype.unique = function() {
      var solution = [];
      for(var i=0; i<this.length; i++) {
        if(solution.indexOf(this[i]) === -1) solution.push(this[i]);
      return solution;
    };
    
  6. unique()
    Array.prototype.unique = function () {
        var r = new Array();
        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;
    ...
    
  7. unique()
    Array.prototype.unique = function() {
      var arr = [];
      for(var i = 0; i < this.length; i++) {
        if(arr.indexOf(this[i]) === -1) {
          arr.push(this[i]);
      return arr;
    };
    ...
    
  8. unique()
    Array.prototype.unique=function () {
        var n=[];
        for(var i=0;i<this.length;i++){
            if(n.indexOf(this[i])==-1){
                n.push(this[i]);
        return n;
    
  9. unique()
    Array.prototype.unique = function () { 
      return this.filter((a, i) => this.indexOf(a) === i)