Nodejs Array Unique uniqueByKey(array, key)

Here you can find the source of uniqueByKey(array, key)

Method Source Code

function getGmtOffset() {
   var d = new Date();
   var gmtOffset = d.getTimezoneOffset() * 60;   // minutes to seconds
   return gmtOffset;
}

function getUnixTime(strDate) {
   var d = new Date(strDate);
   var unixTime = (d.getTime() / 1000);
   return unixTime;
}

Array.prototype.uniqueByKey = function(array, key) {
   // unique = foo.uniqueByKey(bar, "key").sort();
   var r = [];//  ww w . ja v  a2  s  .co  m
   for(var i = 0; i < array.length; i++) {
      if(r.indexOf(array[i][key]) == -1) {
         r.push(array[i][key]);
      }
   }
   return r;
}

Array.prototype.unique = function () {
   // unique = foo.unique().sort();
    var r = [];
    for(var i = 0, n = this.length; i < n; i++) {
        if (this.lastIndexOf(this[i]) == i) r.push(this[i]);
    }
    return r;
}

$.expr[':'].contentIs = function(el, idx, meta) {
    return $(el).text() === meta[3];
};

function isarray(input){
   return typeof(input)=='object'&&(input instanceof Array);
}

Related

  1. unique3()
    Array.prototype.unique3 = function () {
      return this.filter(function (s, i, a) {
        return i===a.lastIndexOf(s);
      })
    };
    
  2. unique3()
    Array.prototype.unique3 = function(){
     var res = [];
     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]]);
    ...
    
  3. 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());
    
  4. 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;
    
  5. 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;
    ...
    
  6. uniqueCounter()
    Array.prototype.uniqueCounter = function () {
        "use strict";
      var counter = 0, l = this.length, i, j;
        for (i = 0; i < l; i += 1) {
        for (j = i + 1; j < l; j += 1) {
          if (this[i] === this[j]) {
            j = i =+ 1;
        counter += 1;
        return counter;
    };
    
  7. uniqueDates()
    Array.prototype.uniqueDates = function() {
      var u = [];
      for (k=0; k<this.length; k++) {
        var currentDate = new Date(this[k].annum);
        if (u.map(Number).indexOf(+currentDate) < 0) {
          u.push(currentDate);
      return u;
    ...
    
  8. uniqueNonEmpty()
    Array.prototype.uniqueNonEmpty = function () {
      var noempties = true;
      var hash = {};
      for (var i=0 ; i<this.length ; i++) {
        if (!this[i] || this[i]=="") {
          noempties = false;
        } else {
          hash[this[i]] = "1";
      var hashlength=0;
      for (i in hash) {hashlength++};
      return (this.length===hashlength && noempties);
    
  9. uniqueObjects(props)
    Array.prototype.uniqueObjects = function (props) {
        function compare(a, b) {
            var prop;
            if (props) {
                for (var j = 0; j < props.length; j++) {
                    prop = props[j];
                    if (a[prop] != b[prop]) {
                        return false;
            } else {
                for (prop in a) {
                    if (a[prop] != b[prop]) {
                        return false;
            return true;
        return this.filter(function (item, index, list) {
            for (var i = 0; i < index; i++) {
                if (compare(item, list[i])) {
                    return false;
            return true;
        });
    };