Nodejs Array Same Structure sameStructureAs(other)

Here you can find the source of sameStructureAs(other)

Method Source Code

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;
    }/*from w  w w  .j  av a  2s .  c  o  m*/
  }
  return true;
};

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) {
      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;
    ...
    
  4. 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( [ [ [ ], [ ] ] ] ))
    ...
    
  5. sameStructureAs(other)
    Array.prototype.sameStructureAs = function(other) {
      if (Array.isArray(other) === false) {
        return false;
      return compareArrays(this, other);
    };
    function compareArrays(arr1, arr2) {
      if (arr1.length !== arr2.length) {
        return false;
    ...
    
  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]);
      });
    };