Nodejs Array Unique Check isUnique(obj)

Here you can find the source of isUnique(obj)

Method Source Code

function rand(min, max){
    min = min || 0;/*from  w w  w.  jav  a 2  s .  c o  m*/
    max = max || 1;

    return Math.random() * (max - min) + min;
};

exports.randInt = function(min, max){
    return Math.floor( rand( min, max ) );
};


//increase Array functionality
//check if snake crashes into own body
Array.prototype.isUnique = function(obj){
  var l = this.length,
  counter = 0;
  for(var i=0; i<l; i++){
     if(this[i].x === obj.x && this[i].y === obj.y){
        counter++;
     }
  }
  return (counter >= 2) ? 0 : 1;
}

//check if snake collides with other snake or item
Array.prototype.collision = function(obj){
  var l = obj.length;
  for(var i=0; i<l; i++){
    var bdy = obj[i].player.body
    if(bdy !== this){
      var str = JSON.stringify(bdy);
      if(str.indexOf(JSON.stringify(this[0])) !== -1){
        return 0;
      }
    }
  }
  return 1;
}

Related

  1. isUnique()
    "use strict";
    Array.prototype.isUnique = function(){
        var that = this;
        var result = true;
        for (var i = 0; i < that.length; i++){
            for (var j = i+1; j < that.length; j++){
                if(that[i] === that[j]){
                    result = false;
        return result;
    };
    
  2. isUnique(value)
    Array.prototype.isUnique = function(value){
      var idx = this.indexOf(value);
      return this.indexOf(value, idx + 1) == -1;
    };