Nodejs Array Unique unique3()

Here you can find the source of unique3()

Method Source Code

Array.prototype.unique3 = function(){
 var res = [];/*from ww  w  . ja  v  a 2 s .  c o m*/
 var json = {};
 var dejson = {};
 var length = 0;
 for(var i = 0; i < this.length; i++){
  if(!json[this[i].name]){
   dejson[this[i]] = this[i];
   console.log(dejson[this[i]]);
   length++;
   json[this[i].name] = 1;
  }else{
     console.log(dejson[this[i]].value);
     dejson[this[i]].value = (dejson[this[i]].value + this[i].value)/2;
     console.log(dejson[this[i]].value);
  }
 }
 console.log(length);
 for(var i = 0; i<length;i++){
    res.push(dejson[this[i]]);
 }
 return res;
}

Related

  1. unique2()
    Array.prototype.unique2=function () {
        var n={};
        var tmp=[];
        for(var i=0;i<this.length;i++){
            if(!n[this[i]]){
                n[this[i]]=true;
                tmp.push(this[i]);
        return tmp;
    
  2. unique2()
    Array.prototype.unique2 = function () {
      var a = [],
        n = this.length;
        for (var i=0; i<n; ++i) {
          for (var j=i+1; j<n; ++j) {
            if(this[i]===this[j]) j = ++i;
          a.push(this[i]);
      return a;
    };
    
  3. unique2()
    Array.prototype.unique2 = function () {
      var result = [];
      for (var i = 0; i < this.length; i++) {
        if (result.indexOf(this[i]) == -1) {
          result.push(this[i]);
      return result;
    
  4. unique2(getKey = val => val)
    Array.prototype.unique2 = function (getKey = val => val) {
      const n = {};
      const r = [];
      for (let i = 0; i < this.length; i += 1) {
        const key = getKey(this[i]);
        if (!n[key]) {
          n[key] = true;
          r.push(this[i]);
      return r;
    };
    
  5. unique3()
    Array.prototype.unique3 = function () {
      return this.filter(function (s, i, a) {
        return i===a.lastIndexOf(s);
      })
    };
    
  6. unique3()
    Array.prototype.unique3 = function(){
      var res = [1,2,3,4,1,2,3,4];
      var json = {};
      for(var i = 0; i < this.length; i++){
          if(!json[this[i]]) {
            res.push(this[i]);
            json[this[i]] = true;
      return res;
    console.log(unique3());
    
  7. unique5()
    Array.prototype.unique5 = function() {
        var res = [], hash = {};
        for(var i=0, elem; (elem = this[i]) != null; i++)  {
            if (!hash[elem])
                res.push(elem);
                hash[elem] = true;
        return res;
    
  8. uniqueArray_unique()
    "use strict";
    Array.prototype.unique = function Array_unique() {
        var unique = [];
        for (var i = 0; i < this.length; i++) {
            if (unique.indexOf(this[i]) === -1) {
                unique.push(this[i]);
        return unique;
    ...
    
  9. uniqueByKey(array, key)
    function getGmtOffset() {
      var d = new Date();
      var gmtOffset = d.getTimezoneOffset() * 60;  
      return gmtOffset;
    function getUnixTime(strDate) {
      var d = new Date(strDate);
      var unixTime = (d.getTime() / 1000);
      return unixTime;
    ...