Nodejs Array Unique unique

Here you can find the source of unique

Method Source Code

/*/*from w ww . j  a  va 2s .  c o  m*/
 * Extend some basic JavaScript clases with some helper methods.
 */

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] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

// For todays date;
Date.prototype.today = function () {
    return ((this.getDate() < 10)?"0":"") + this.getDate() +"."+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"."+ this.getFullYear();
}

// For the time now
Date.prototype.timeNow = function () {
     return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}

Related

  1. uniq()
    function uniq(array) {
      let newArray = [];
      for (let i = 0; i < array.length; i++) {
        if (!newArray.includes(array[i])) {
          newArray.push(array[i]);
      return newArray;
    Array.prototype.uniq = function () {
      let newArray = [];
      for (let i = 0; i < this.length; i++) {
        if (!newArray.includes(this[i])) {
          newArray.push(this[i]);
      return newArray;
    };
    Array.prototype.twoSum = function () {
      let positions = [];
      for (let i = 0; i < this.length; i++) {
        for (let j = i + 1; j < this.length; j++) {
          if (this[i] + this[j] === 0) {
            positions.push([i, j]);
      return positions;
    };
    function myTranspose (array) {
      let transposed = [];
      for (let i = 0; i < array.length; i++) {
        let row = [];
        for (let j = 0; j < array.length; j++) {
          row.push(array[j][i]);
        transposed.push(row);
      return transposed;
    console.log(myTranspose([
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
      ]));
    
  2. uniq()
    function uniq(array) {
      const beenSeen = [];
      for (let i = 0; i < array.length; i++) {
        if (!(beenSeen.includes(array[i]))) {
          beenSeen.push(array[i]);
      return beenSeen;
    Array.prototype.uniq = function() {
      const beenSeen = [];
      for (let i = 0; i < this.length; i++) {
        if (!(beenSeen.includes(this[i]))) {
          beenSeen.push(this[i]);
      return beenSeen;
    };
    Array.prototype.two_sum = function() {
      const twoSum = [];
      for (let i = 0; i < this.length - 1 ; i++) {
        for (let j = i + 1; j < this.length; j++) {
          if (this[i] + this[j] === 0) {
            twoSum.push([i , j]);
      return twoSum;
    };
    function myTranspose(matrix) {
      const outLength = matrix[0].length;
      const outWidth = matrix.length;
      const outArray = [];
      for (let i = 0; i < outLength; i++) {
        const row = [];
        for (let j = 0; j < outWidth; j++) {
          row.push(matrix[j][i]);
        outArray.push(row);
      return outArray;
    console.log(myTranspose([
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
      ]));
    
  3. uniq()
    Array.prototype.uniq = function() {
      var uniqArray = [];
      for (var i = 0; i < this.length; i++) {
        if (uniqArray.indexOf(this[i]) === -1) {
          uniqArray.push(this[i]);
      return uniqArray;
    };
    ...
    
  4. 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]);
        return a;
      };
    
  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]);
        return a;
      };
      Array.prototype.clean = function(deleteValue) {
        for (var i = 0; i < this.length; i++) {
          if (this[i] == deleteValue) {         
            this.splice(i, 1);
            i--;
        return this;
      };  
    String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
    
  6. unique()
    Array.prototype.unique = function() {
        return [... new Set(this)]
    
  7. unique()
    Array.prototype.unique=function(){
        return this.filter(function (x,i,self){
            return self.indexOf(x)===i;
        });
    
  8. unique()
    Array.prototype.unique = function() {
       return this.sort().reduce( (a,e) => e === a[a.length-1] ? a : (a.push(e), a), [] )
    
  9. unique()
    Array.prototype.unique=function(){
      var n=[];
      for(var i=0;i<this.length;i++){
        if(n.indexOf(this[i])==-1) n.push(this[i]);
      return n;