Javascript Array compare(array)

Description

Javascript Array compare(array)


// Compare two arrays - from: http://stackoverflow.com/a/14853974
Array.prototype.compare = function (array) {
  if (!array)//from ww  w  . j  ava 2 s .  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].compare(array[i]))
      return false;
    }
    else if (this[i] != array[i]) {
      return false;
    }
  }
  return true;
}

Javascript Array compare(other)

Array.prototype.compare = function(other) {
  return this.join('') == other.join('');
};

Javascript Array compare(other)

Array.prototype.compare = function(other) {
    var diff = [];
    for ( var i = 0; i < this.length; i++) {
        if (other.indexOf(this[i]) < 0) {
            diff.push(this[i]);//from w ww.  j  a v a2s .co m
        }
    }
    return diff;
}

Javascript Array compare(testArr)

Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { 
            if (!this[i].compare(testArr[i])) return false;
        }// w  w  w  .  j  av a  2  s .  co  m
        if (this[i] !== testArr[i]) return false;
    }
    return true;
}



PreviousNext

Related