Nodejs Array Unique uniq()

Here you can find the source of uniq()

Method Source Code

Array.prototype.uniq = function () {
  var uniques = [];
  this.forEach(function(element){
    var match = false;

    uniques.forEach(function(uniqElement){
      if (uniqElement === element){
        match = true;// ww  w  .j  a  v a 2 s .c  o  m
      }
    });

    if (match === false){ uniques.push(element) }

  });
    // if (uniques.find(element) === undefined)
    // uniques.push(element);
  return uniques;
};
var array = [1,2,3,4,5,5];
var uniqs = array.uniq();

console.log(array);
console.log(uniqs);

Related

  1. uniq()
    Array.prototype.uniq = function() {
      return this.filter(function(value, index) {
        return this.indexOf(value) == index;
      }.bind(this));
    };
    
  2. uniq()
    Array.prototype.uniq = function(){
      var uniqueness = [];
      for(var i = 0; i < this.length; i++){
        var element = this[i];
        if (this.indexOf(element) === this.lastIndexOf(element) || uniqueness.indexOf(element) === -1){
          uniqueness.push(this[i]);
      return uniqueness;
    ...
    
  3. uniq()
    Array.prototype.uniq = function() {
      var u = {}, a = [];
      for (var i = 0, ii = this.length; i < ii; i++) {
        if (u.hasOwnProperty(this[i])) {
          continue;
        a.push(this[i]);
        u[this[i]] = 1;
      return a;
    };
    
  4. uniq()
    Array.prototype.uniq = function(){
        var ans = [];
        var hasNaN = false;
        for(var i = 0,len = this.length; i < len; i++){
            if(ans.indexOf(this[i]) == -1){
                if(this[i] != this[i]){
                    if(!hasNaN){
                        ans.push(this[i]);
                        hasNaN = true;
    ...
    
  5. uniq()
    Array.prototype.uniq = function(){
      var uniques = [];
      for(var i = 0; i < this.length; i++){
        if( uniques.indexOf(this[i]) === -1){
          uniques.push(this[i]);
      return uniques;
    };
    ...
    
  6. uniq()
    Array.prototype.uniq = function () {
      var arr = [];
      var flag = true;
      this.forEach(function (item) {
        if (item != item) {
          flag && arr.indexOf(item) === -1 ? arr.push(item) : '';
          flag = false;
        } else {
          arr.indexOf(item) === -1 ? arr.push(item) : ''
    ...
    
  7. uniq()
    Array.prototype.uniq = function () {
      var uniq = [];
      for (var i = 0; i < this.length; i++) {
        if (uniq.indexOf(this[i]) === -1) {
          uniq.push(this[i]);
      return uniq;
    };
    ...
    
  8. uniq()
    Array.prototype.uniq = function () {
      const uniqArr = [];
      for (let i = 0; i < this.length; i++) {
        if (!uniqArr.includes(this[i])) {
          uniqArr.push(this[i]);
      return uniqArr;
    };
    ...
    
  9. uniq()
    Array.prototype.uniq = function() {
      var uniq_array = [];
      for (var i=0; i<this.length; i++) {
        curr_ele = this[i]
        if (this.lastIndexOf(curr_ele) === i) {
          uniq_array.push(curr_ele);
      return uniq_array;
    ...