Nodejs Array Same Structure sameStructureAs(other)

Here you can find the source of sameStructureAs(other)

Method Source Code

Array.prototype.sameStructureAs = function(other) {
  if (Array.isArray(other) === false) {
    return false;
  }//from  w  w w. j a  va2  s  . c  om
  return compareArrays(this, other);
};

function compareArrays(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }
  for (let i = 0, len = arr1.length; i < len; i++) {
    if (Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
      return compareArrays(arr1[i], arr2[i]);
    } else if (
      (Array.isArray(arr1[i]) && !Array.isArray(arr2[i])) ||
      (!Array.isArray(arr1[i]) && Array.isArray(arr2[i]))
    ) {
      return false;
    }
  }
  return true;
}

export default Array;

Related

  1. sameStructureAs(a2)
    Array.prototype.sameStructureAs = function(a2) {
      let isSame = true;
      (function recurse(arr1, arr2){
        if (!arr1 || !arr2) {
          isSame = false;
          return;
        if (arr1.length !== arr2.length) {
          isSame = false;
    ...
    
  2. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {
      const stringify = a => `[${a.map(b => Array.isArray(b) ? stringify(b) : '|').join('')}]`
      return Array.isArray(other) ? stringify(this) === stringify(other) : false
    
  3. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {
      if ( this.length !== other.length ) return false;
      for ( var i = 0; i < this.length; i++ ){
        if (Array.isArray(this[i]) && Array.isArray(other[i])) {
          if (!this[i].sameStructureAs(other[i])) return false;
        } else if (Array.isArray(this[i]) || Array.isArray(other[i])){
          return false;
      return true;
    };
    
  4. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {
      var helper = function(a,b){
        if(!isArray(b))
          return false;
        for(i in a){
          if(b[i]===undefined || isArray(a[i])!==isArray(b[i]) || isArray(a[i]) && !a[i].sameStructureAs(b[i]))
            return false;
        return true;
    ...
    
  5. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {   
      if (!Array.isArray(other)) return false;
      function transform(array) {
        return array.map(item => Array.isArray(item) ? [transform(item)] : "0");
      return JSON.stringify(transform(this)) === JSON.stringify(transform(other));
    };
    console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ 2, [ 2, 2 ] ] ));
    console.log([ [ [ ], [ ] ] ].sameStructureAs( [ [ [ ], [ ] ] ] ))
    ...
    
  6. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {
      return this && other && Array.isArray(this) === Array.isArray(other) && this.every(function(element, i) {
        if (Array.isArray(element)) {
          return element.sameStructureAs(other[i]);
        return element && other[i] && Array.isArray(element) === Array.isArray(other[i]);
      });
    };
    
  7. sameStructureAs(other)
    Array.prototype.sameStructureAs = function (other) {
      if(other.constructor !== Array){
        return false;
      } else {
        emptyNested(this);
        emptyNested(other);
        var arr1Str = JSON.stringify(this);
        var res1 = '';
        var arr2Str = JSON.stringify(other);
    ...