JSFiddle - React, Tailwind, and code Playground
by Artem
Babel + JSX
'use strict';
const traverse = (source, target) => {
if (source.length !== target.length) {
return false;
}
const res = source.every((el, idx) => {
if (!Array.isArray(el) && !Array.isArray(target[idx])) {
return true;
} else if (Array.isArray(el) && Array.isArray(target[idx])) {
return traverse(el, target[idx]);
} else {
return false;
}
});
return res;
};
Array.prototype.sameStructureAs = function(other) {
return traverse(this, other);
};
console.log([ 1, 1, 1 ].sameStructureAs( [ 2, 2, 2 ] ));
console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ 2, [ 2, 2 ] ] ));
console.log([ 1, [ 1, 1 ] ].sameStructureAs( [ [ 2, 2 ], 2 ] ));