Nodejs Array Same Structure sameStructureAs(other)

Here you can find the source of sameStructureAs(other)

Method Source Code

/*//  w ww .j  a v  a  2  s.  c o  m

Complete the method on Array called 'sameStructureAs' to return true when its 
argument is an Array that has the same nesting structure as the 'this' array.

For example:

 // should return true
[ 1, 1, 1 ].sameStructureAs( [ 2, 2, 2 ] );          
[ 1, [ 1, 1 ] ].sameStructureAs( [ 2, [ 2, 2 ] ] );  

 // should return false 
[ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2, 2 ], 2 ] );  
[ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2 ], 2 ] );  

// should return true
[ [ [ ], [ ] ] ].sameStructureAs( [ [ [ ], [ ] ] ] ); 

// should return false
[ [ [ ], [ ] ] ].sameStructureAs( [ [ 1, 1 ] ] );
For your convenience, there is already a function 'isArray(o)' declared that 
returns whether its argument is an array.

*/

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);
    var res2 = '';
    for(var i = 0; i < arr1Str.length; i++){
      var cur = arr1Str[i];
      if(cur === '['){
        res1 += 1;
      } else if (cur === ']'){
        res1 += 2;
      } else if (cur === ','){
        res1 += 3;
      }
    }
    for(var i = 0; i < arr2Str.length; i++){
      var cur = arr2Str[i];
      if(cur === '['){
        res2 += 1;
      } else if (cur === ']'){
        res2 += 2;
      } else if (cur === ','){
        res2 += 3;
      }
    }
    return res2 === res1;
  }
};

function emptyNested(array){
  for(var i = 0; i < array.length; i++){
    if(array[i].constructor === Array){
      emptyNested(array[i]);
    } else {
      delete array[i]
    }
  }
}
var kia = ['a', 'b', 'c', ['a', 'b', 'c', ['a', 'b', 'c', ['a', 'b', 'c']]]];
emptyNested(kia);
console.log(JSON.stringify(kia))


console.log([ 1, 1, 1 ].sameStructureAs( [ 2, 2, 2 ] ));          
console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ 2, [ 2, 2 ] ] ));  

 // should return false 
console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2, 2 ], 2 ] ));  
console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2 ], 2 ] ));  

// should return true
console.log([ [ [','], [ ] ] ].sameStructureAs( [ [ [ ], [ ] ] ]) );

//false
console.log([ [ [ ], [ ] ] ].sameStructureAs( [ [ 1, 1 ] ] ));

Related

  1. 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;
    };
    
  2. 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;
    ...
    
  3. 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( [ [ [ ], [ ] ] ] ))
    ...
    
  4. 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;
    ...
    
  5. 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]);
      });
    };