Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

function SecondGreatLow(arr) {
 arr.sort(function (a,b){
   return a-b;/* ww w  . j  ava 2  s .c  om*/
 });
 arr = arr.unique();
 if (arr.length == 2){
    return arr[1]+ " "+ arr[0];
 } else {
    return arr[1]+ " " + arr[arr.length-2];
 }
}
Array.prototype.unique = function() {
    return this.reduce(function(accum, current) {
        if (accum.indexOf(current) < 0) {
            accum.push(current);
        }
        return accum;
    }, []);
}

console.log(SecondGreatLow([2, 2,2,42, 42, 180]));

var r = [1,2,2,3,24,3,453,5,55,65,,6,57677,7,7,7,878,8].unique();
console.log(r)

Related

  1. unique()
    Array.prototype.unique = function () { 
      return this.filter((a, i) => this.indexOf(a) === i) 
    
  2. unique()
    Array.prototype.unique = function () {
      var arr = this;
      return this.filter(function (el, idx) {
        return arr.lastIndexOf(el) === idx;
      });
    };
    
  3. unique()
    Array.prototype.unique = function () {
      var r = [];
      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;
        r[r.length] = this[i];
    ...
    
  4. unique()
    Array.prototype.unique = function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
        return a;
    ...
    
  5. unique()
    function getDomain(address) {
      return address.substring(address.indexOf('@') + 1);
    function cleanPhoneNo(str) {
      return str.replace(/[^\d]/g, '');
    function escapeRegExp(str) {
      var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); 
      return str.replace(specials, "\\$&");
    ...
    
  6. unique()
    Array.prototype.unique = function() {
        if (Object.prototype.toString.call(this) !== '[object Array]') {
            throw new TypeError("`this` must be Array, not " + typeof this);
        var unique = [];
        for (var i = 0, l = this.length; i < l; i++) {
            var element = this[i];
            if (unique.indexOf(element) === -1) {
                unique.push(element);
    ...
    
  7. unique()
    Array.prototype.unique = function(){
      var r = new Array();
      o:for (var i = 0; i < this.length; i++) {
        for (var x = 0; x < r.length; x++) {
          if (r[x]==this[i]) continue o;
        r[r.length] = this[i];
      return r;
    ...
    
  8. unique()
    Array.prototype.unique = function() {
      var o = {}, i, l = this.length, r = [];
      for(i=0; i<l;i+=1) o[this[i]] = this[i];
      for(i in o) {
        if (o[i]) {
          r.push(o[i]);
      return r;
    ...
    
  9. unique()
    Array.prototype.unique = function() {
      return this.filter( function(value, index, self) { 
        return self.indexOf(value) === index;
      } );
    };