Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

var numbers = [4,5,4,4,4,5,4,5]

// Create a function that returns
// an array of unique numbers

// Here's one way
function unique(arr) {
  var newArr = [];
  for (var i = 0; i < arr.length; i++) {
    //console.log(newArr.indexOf(arr[i]));
    if (newArr.indexOf(arr[i]) < 0) {
      newArr.push(arr[i]);/*from   ww w.  j  av  a2  s .c  o m*/
    }
  }
  return newArr;
}

if (this || that && notThis)


Array.prototype.unique = function () {
  var newArr = [];
  for (var i = 0; i < this.length; i++) {
    //console.log(newArr.indexOf(arr[i]));
    if (newArr.indexOf(this[i]) < 0) {
      newArr.push(this[i]);
    }
  }
  return newArr;
}


var unique = function(arr) {
  return arr.filter(function(x, i) {
    return arr.indexOf(x) === i
  })
}

Related

  1. unique()
    Array.prototype.unique = function(){
        var array = [];
        for(var i = 0; i < this.length; i++) {
            if(!array.contains(this[i])) {
                array.push(this[i]);
        return array.compact();
    };
    ...
    
  2. unique()
    Array.prototype.unique = function() {
      var uniques = [];
      this.forEach(function(value) {
        if (uniques.indexOf(value) == -1)
          uniques.push(value);
      });
      return uniques;
    };
    
  3. unique()
    Array.prototype.unique = function() {
      return this.reduce(function(prev_val, current_val) {
        if (prev_val.indexOf(current_val) < 0) {
          prev_val.push(current_val);
        return prev_val;
      }, []);
    };
    Array.prototype.times_do = function(callback) {
    ...
    
  4. unique()
    Array.prototype.unique = function() {
        var a = [];
        for (var i = 0, l = this.length; i < l; i++)
            if (a.indexOf(this[i]) === -1)
                a.push(this[i]);
        return a;
    
  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 u = {}, a = [];
       for(var l = 0, i = this.length-1; i >= 0; --i){
          if(u.hasOwnProperty(this[i])) {
             continue;
          a.unshift(this[i]);
          u[this[i]] = 1;
       return a;
    };
    
  7. unique()
    Array.prototype.unique = function() {
      var key, output, value, _ref, _results;
      output = {};
      for (key = 0, _ref = this.length; 0 <= _ref ? key < _ref : key > _ref; 0 <= _ref ? key++ : key--) {
        output[this[key]] = this[key];
      _results = [];
      for (key in output) {
        value = output[key];
    ...
    
  8. unique()
    Array.prototype.unique = function () {
        return this.filter(function (value, index, array) {
            return array.indexOf(value) === index;
        });
    };
    
  9. unique()
    'use strict';
    Array.prototype.unique = function() {
      var newArray = [];
      for (var i = 0, l = this.length; i < l; i++) {
        if (newArray.indexOf(this[i]) === -1 && this[i] !== '') {
          newArray.push(this[i]);
      return newArray;
    ...