Nodejs Array Filter filterUnique()

Here you can find the source of filterUnique()

Method Source Code

/*----------------------------------------------------------------------------*\
    # Copyright 2017, BuzzingPixel, LLC//from  ww w.  j  a v  a  2 s  .c  o m

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the Apache License 2.0.
    # http://www.apache.org/licenses/LICENSE-2.0
\*----------------------------------------------------------------------------*/

Array.prototype.filterUnique = function() {
    'use strict';

    var arr = [];
    var orig = this;

    orig.forEach(function(a, i) {
        if (arr.indexOf(a) < 0) {
            arr.push(a);
        } else {
            orig.splice(i, 1);
        }
    });

    return orig;
};

Array.prototype.getUnique = function() {
    'use strict';

    var arr = [];

    this.forEach(function(a) {
        if (arr.indexOf(a) < 0) {
            arr.push(a);
        }
    });

    return arr;
};

Array.prototype.pushUnique = function() {
    'use strict';

    for (var i = 0; i < arguments.length; ++i) {
        if (this.indexOf(arguments[i]) < 0) {
            this.push(arguments[i]);
        }
    }

    return this;
};

Related

  1. customFilter(fn)
    Array.prototype.customFilter = function(fn) {
      let results = [];
        for (let i = 0; i < this.length; i++) {
            let itemExists = fn(this[i]);
            if (itemExists) {
                results.push(this[i]);
      return results;
    ...
    
  2. filterByField(field, key)
    Array.prototype.filterByField = function(field, key) {
      var newArray = [];
      for (var i = 0; i < this.length; i++) {
        if (this[i][field] == key) {
          newArray.push(this[i]);
      return newArray;
    };
    ...
    
  3. filterByField(field, value)
    Array.prototype.filterByField = function (field, value)
        var list = this;
        var listReturn = [];
        list.forEach(function(item)
            if (item[field] === value)
                listReturn.push(item);
    ...
    
  4. filterEmpty()
    Array.prototype.filterEmpty = function ()
        var arr = this;
        var i = arr.length;
        while (i--)
            if (arr[i] instanceof Array)
                arr[i] = arr[i].filterEmpty();
    ...
    
  5. filterSyncasync (callback, thisArg)
    Array.prototype.filterSync = async function (callback, thisArg) {
      let filterResult = await Promise.all(this.map(callback))
      return this.filter((_, index) => filterResult[index])
    
  6. mfilter(fun)
    Array.prototype.mfilter =  function (fun) {
      var filtered = [];
      for(let i = 0; i < this.length; i++) {
        if (fun(this[i], i, this)) filtered.push(this[i]);
      return filtered;
    };
    var returnarr = [1,2,3,4,5,6].mfilter(function(element, index, arr) {
      return element > 5;
    ...
    
  7. single(filter)
    Array.prototype.single = function(filter) {
      var matches = this.filter(filter);
      if (matches.length == 0) {
        throw new Error("Expected 1 but found 0");
      if (matches.length > 1) {
        throw new Error("Expected 1 but found many");
      return matches[0];
    ...
    
  8. singleOrNone(filter)
    Array.prototype.singleOrNone = function(filter) {
      var matches = this.filter(filter);
      if (matches.length > 1) {
        throw new Error("Expected 1 but found many");
      if (matches.length == 0) {
        return null;
      return matches[0];
    ...