Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

var fs = require('fs');

Array.prototype.unique = function() {
   var res = [],/*from ww  w  . j  a  va2s .  c o m*/
      hash = {};
   for (var i = 0, elem;
      (elem = this[i]) != null; i++) {
      if (!hash[elem]) {
         res.push(elem);
         hash[elem] = true;
      }
   }
   return res;
};
Array.prototype.superUnique = function() {
   var res = [],
      hash = {};
   for (var i = 0; i < this.length - 1; i++) {
      var elem = this[i];
      var compareEle = JSON.parse(this[i]).tel.toString() + JSON.parse(this[i]).email.toString() + JSON.parse(this[i]).jobDes.toString();
      if (!hash[compareEle]) {
         res.push(elem);
         hash[compareEle] = true;
      }
   }
   return res;
};
var targetArr = [];
fs.readFile('zlzp.json', 'utf-8', function(err, data) {
   targetArr = data.split('$%').unique();
   fs.writeFile('zlzp_goal.json', targetArr);
})

Related

  1. 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;
    };
    
  2. 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];
    ...
    
  3. unique()
    Array.prototype.unique = function () {
        return this.filter(function (value, index, array) {
            return array.indexOf(value) === index;
        });
    };
    
  4. 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;
    ...
    
  5. unique()
    Array.prototype.unique = function() {
      var a = [];
      var l = this.length;
      for ( var i = 0; i < l; i++) {
        for ( var j = i + 1; j < l; j++) {
          if (this[i] === this[j])
            j = ++i;
        a.push(this[i]);
    ...
    
  6. unique()
    Array.prototype.unique = function() {
      var seen = {}
      return this.filter(function(x) {
        if (seen[x])
          return
        seen[x] = true
        return x
      })
    
  7. unique()
    Array.prototype.unique = function() {
      var array = [];
      var l = this.length;
      for(var i=0; i<l; i++) {
        for(var j=i+1; j<l; j++) {
          if (this[i] === this[j]) j = ++i;
        array.push(this[i]);
      return(array);
    };
    
  8. unique()
    Array.prototype.unique = function () {
      var r = new Array();
      o:for(var i = 0, n = this.length; i < n; i++)
        for(var x = 0, y = r.length; x < y; x++)
          if(r[x]==this[i])
            continue o;
    ...
    
  9. unique()
    Array.prototype.unique = function() {
        return this.reduce(function(p, c) { 
                        if (p.indexOf(c) < 0) p.push(c);
                        return p;
                    }, []);