Javascript Array equals(array)

Description

Javascript Array equals(array)


/**/*from  w w w .ja v  a  2s . c  o m*/
 * Checks whether the two arrays are same.
 *
 * @method Array.prototype.equals
 * @param {Array} array The array to compare to
 * @return {Boolean} True if the two arrays are same
 */
Array.prototype.equals = function(array) {
    if (!array || this.length !== array.length) {
        return false;
    }
    for (var i = 0; i < this.length; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].equals(array[i])) {
                return false;
            }
        } else if (this[i] !== array[i]) {
            return false;
        }
    }
    return true;
};

Javascript Array equals(array)

Array.prototype.equals = function(array) {
  if (!array)// w w  w.  java 2  s. c o m
    return false;

  if (this.length !== array.length)
    return false;

  return this.every((x, i) => x === array[i]);
};

const a1 = [1,2,3];
const a2 = [1,2,3];

maxCount = 100;

console.log(a1.equals(a2));

Javascript Array equals(array)

// Compare two arrays
Array.prototype.equals = function (array) {
    for(var i = 0; i < this.length; i++){
        if(this[i] != array[i]){
            return false;
        }/*from   w ww  .jav a 2 s. c o  m*/
    }
    return true;
}

Javascript Array equals(array)

Array.prototype.equals = function (array) {
    if (!array)//  w  w  w. j  a v  a  2s .  co  m
        return false;
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) {
            return false;   
        }           
    }       
    return true;
}

Javascript Array equals(array)

Array.prototype.equals = function (array) {
    if (!array)/*from  ww w.j  a v a 2 s. c o  m*/
        return false;
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].equals(array[i]))
                return false;
        }
        else if (this[i] != array[i]) {
            return false;
        }
    }
    return true;
};

Javascript Array equals(array)

Array.prototype.equals = function (array) {
    if (!array){//w w w. j  a v  a2  s . c  om
        return false;
    }

    if (this.length != array.length){
        return false;
    }

    for (var i = 0, l=this.length; i < l; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].compare(array[i])){
                return false;
            }
        }
        else if (this[i] != array[i]) {
            return false;
        }
    }
    return true;
};

var randomBoolean = function() {
     return Math.random() < 0.5;
}

Javascript Array equals(array)

Array.prototype.equals = function (array) {
 // if the other array is a falsy value, return
 if (!array)//from   w  ww .j a v  a  2s. c o  m
  return false;

 // compare lengths - can save a lot of time 
 if (this.length != array.length)
  return false;

 for (var i = 0, l=this.length; i < l; i++) {
  // Check if we have nested arrays
  if (this[i] instanceof Array && array[i] instanceof Array) {
   // recurse into the nested arrays
   if (!this[i].equals(array[i]))
    return false;
  }
  else if (this[i] != array[i]) { 
   // Warning - two different object instances will never be equal: {x:20} != {x:20}
   return false;
  }
 }
 return true;
}

Javascript Array equals(array)

Array.prototype.equals = function (array) {
    if (!array) return false;
    if (this.length != array.length) return false;
    for (var i = 0, l = this.length; i < l; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array) {
            if (!this[i].equals(array[i])) {
                return false;
            }/*  w w w  .ja v a  2  s.c  o  m*/
        } else if (this[i] != array[i]) {
            return false;
        }
    }
    return true;
};
Object.defineProperty(Array.prototype, 'equals', {enumerable: false});
function test_equals(a, b) {
    return a.equals(b);
}

Javascript Array equals(array)

/**/*from  ww  w.  ja v a2 s  . c om*/
 * Expand built-in Array
 */

/* Check for arrays equality */
Array.prototype.equals = function(array) {
  // check argument presence
  if (!array)
    return false;

  // compare lengths
  if (this.length != array.length)
    return false;

  for (var i = 0, l = this.length; i < l; i++) {
    // Check for nested arrays
    if (this[i] instanceof Array && array[i] instanceof Array) {
      // recurse into the nested arrays
      if (!this[i].equals(array[i]))
        return false;       
    }           
    else if (this[i] != array[i]) { 
      return false;   
    }           
  }       
  return true;
}  

module.exports = Array;

Javascript Array equals(array)

Array.prototype.equals = function(array) {
  var answer = this.every(function(element, index) {
    return element === array[index];
  });/*from www.  j a  v a 2s.c o  m*/

  if (answer) {
    return answer;
  } else {
    return 'Not equal!\n' + 'Expected: [' + array + ']\n' + 'Got: [' + this + ']';
  }
}

function merge(array1, array2) {
  var copy1 = array1.slice();
  var copy2 = array2.slice();
  var merged = [];

  while (copy1[0] || copy2[0]) {
    if (copy1[0] < copy2[0] || typeof copy2[0] === 'undefined') {
      merged.push(copy1.shift());
    } else {
      merged.push(copy2.shift());
    }
  }

  return merged;
}

console.log(merge([1, 5, 9], [2, 6, 8]).equals([1, 2, 5, 6, 8, 9]));
console.log(merge([1, 1, 3], [2, 2]).equals([1, 1, 2, 2, 3]));
console.log(merge([], [1, 4, 5]).equals([1, 4, 5]));
console.log(merge([1, 4, 5], []).equals([1, 4, 5]));

Javascript Array equals(array)

Array.prototype.equals = function (array) {
  if (!array)/*from  w  ww  .j  av  a 2s  .  c o m*/
    return false;

  if (this.length != array.length)
    return false;

  for(var i = 0, l = this.length; i < l; i++) {
    if (this[i] instanceof Array && array[i] instanceof Array){
      if (!this[i].equals(array[i]))
      return false;
    }
    else if (this[i] != array[i]) {
      return false;
    }
  }
    return true;
};
Object.defineProperty(Array.prototype, "equals", {enumerable: false});


var string = 'string';
var array1 = [1,2,3];
var array2 = [1,2,3];
var array3 = [1,[2,2],3];
var array4 = [1,[2,2],3];

console.log('expect false: ' + array1.equals(string));
console.log('expect true: ' + array1.equals(array2));
console.log('expect true: ' + array1.equals(array1));
console.log('expect false: ' + array2.equals(array3));
console.log('expect true: ' + array3.equals(array4));
console.log('expect false: ' + array2.equals(null));

// console.log(Object.getOwnPropertyNames(string));

Javascript Array equals(array)

// http://stackoverflow.com/a/14853974
Array.prototype.equals = function (array)
{
    if (!array)/*from w ww.  j  a v a2  s.c  o m*/
    {
        return false;
    }

    if (this.length != array.length)
    {
        return false;
    }

    for (var i = 0, l=this.length; i < l; i++) {
        if (this[i] instanceof Array && array[i] instanceof Array)
        {
            if (!this[i].equals(array[i]))
            {
                return false;
            }
        }
        else if (this[i] != array[i])
        {
            return false;
        }
    }
    return true;
};



PreviousNext

Related