Nodejs Array Distinct distinct()

Here you can find the source of distinct()

Method Source Code

Array.prototype.distinct = function () {
    var sameObj = function (a, b) {
        var tag = true;
        if (!a || !b) return false;
        for (var x in a) {
            if (!b[x])
                return false;
            if (typeof (a[x]) === 'object') {
                tag = sameObj(a[x], b[x]);
            } else {/*  ww  w  .j a  v  a 2s. co m*/
                if (a[x] !== b[x])
                    return false;
            }
        }
        return tag;
    }
    var newArr = [], obj = {};
    for (var i = 0, len = this.length; i < len; i++) {
        if (!sameObj(obj[typeof (this[i]) + this[i]], this[i])) {
            newArr.push(this[i]);
            obj[typeof (this[i]) + this[i]] = this[i];
        }
    }
    return newArr;
}

Related

  1. distinct()
    Array.prototype.distinct = function(){
      for(var i = 0;i < this.length;i++){
        for(var j = i+1;j < this.length;){
          if(this[j] === this[i]){
          for(var k = j;k < this.length;k++){
            this[k] = this[k + 1];
             delete this[this.length - 1];
            this.length--;
    ...
    
  2. distinct()
    Array.prototype.distinct = function() {
        var hash = {};
        var a = [];
        for(var i = 0; i < this.length; i++) {
            if(hash.hasOwnProperty(this[i])) {
                continue;
            hash[this[i]] = true;
            a.push(this[i]);
    ...
    
  3. distinct()
    Array.prototype.distinct = function () {
        var result = new Array();
        this.forEach(x => {
            if (!result.contains(x))
                result.push(x);
        });
        return result;
    };
    
  4. distinct()
    Array.prototype.distinct = function(){
      var self = this
      var _array = []
      self.forEach(function(e){
        if(_array.contains(e)){
          _array.push(e)
      })
      return _array
    ...
    
  5. distinct()
    Array.prototype.distinct = function () {
      var result = [];
      var hash = new Dictionary();
      forEach(this, function (element) {
        if (!hash.contains(element)) {
          result.push(element);
          hash.store(element, element);
      });
    ...
    
  6. distinct(comparer)
    Array.prototype.distinct = function (comparer) {
      var arr = [];
      var l = this.length;
      for (var i = 0; i < l; i++) {
        if (!arr.contains(this[i], comparer))
          arr.push(this[i]);
      return arr;
    };
    ...
    
  7. distinct2()
    Array.prototype.distinct2 =  function(){
      var _self = this;
      var _a = this.concat().sort(); 
      console.log(_a);
      _a.sort(function(a,b){
        console.log("a: "+ a, "b: "+ b);
        if(a == b){
          var n = _self.indexOf(a);
          _self.splice(n, 1);
    ...