Nodejs Array Compare compare(array)

Here you can find the source of compare(array)

Method Source Code

/**/*ww  w  .j a  va2s . c om*/
 * Recursively compare an array to another array.
 *
 * @param array array Array to compare with
 * @return boolean
 * @author http://stackoverflow.com/a/14853974/2113023
 */
Array.prototype.compare = function (array) {
   // if the other array is a falsy value, return
   if (!array)
      return false;

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

   for (var i = 0; i < this.length; i++) {
      // Check if we have nested arrays
      if (this[i] instanceof Array && array[i] instanceof Array) {
         // recurse into the nested arrays
         if (!this[i].compare(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;
}

Related

  1. compare(array)
    var _ = require('underscore');
    Array.prototype.compare = function(array) {
      if(this.length !== array.length) return false;
      for(var i = 0; i < array.length; i++) {
        if(!_.contains(this, array[i]))
          return false;
      };
      return true;
    };
    ...
    
  2. compare(array)
    Array.prototype.compare = 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].compare(array[i]))
          return false;
    ...
    
  3. compare(array)
    Array.prototype.compare = function (array) {
      if (!array)
        return false;
      if (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].compare(array[i]))
            return false;
    ...
    
  4. compare(array)
    Array.prototype.compare = function (array) {
        if (!array)
            return false;
        if (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].compare(array[i]))
                    return false;
    ...
    
  5. compare(array)
    Array.prototype.compare = 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].compare(array[i]))
                    return false;
    ...
    
  6. compare(array)
    Array.prototype.compare = function (array) {
        if (!array)
            return false;
        if (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].compare(array[i]))
                    return false;
    ...
    
  7. compare(array)
    Array.prototype.compare = function (array) {
      if (!array)
        return false;
      if (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].compare(array[i]))
            return false;
    ...
    
  8. compare(other)
    Array.prototype.compare = function(other) {
      return this.join('') == other.join('');
    };
    
  9. compare(secondArray, compareFunction)
    Array.prototype.compare = function(secondArray, compareFunction){
        if(this.length != secondArray.length){
            return false;
        for(var i = 0; i < this.length; i++){
            if(this[i] instanceof Array && secondArray instanceof Array){
                this[i].compare(secondArray[i]);
            }else if(this[i] instanceof Object && secondArray instanceof Object){
                if(!compareFunction(this[i], secondArray[i])){
    ...