JSFiddle - React, Tailwind, and code Playground

by Jon-Carlos Rivera

JavaScript

var obj1 = {
    foo: 123,
    bar: "test",
    baz: {
        nested: "obj"
    }
};

var obj2 = {
    bar: "evil",
    foo: 666,
    baz: {
        nested: "another"
    }
};

var stop = false;
var type = Object.prototype.toString;

function toKeys(o, prefix) {
    if (stop) return;
    if (typeof prefix !== 'string') prefix = "";
    var keys = Object.keys(o);
    keys.forEach(function(key){
        if (type.call(o[key]) === '[object Object]')
            keys = keys.concat(toKeys(o[key], key + "."));
    });
    return keys.sort().map(function(v){ return prefix + v;});
}

function sameStructure(o1, o2) {
    var o1Keys = toKeys(o1).toString();
    var o2Keys = toKeys(o2).toString();
    console.log(o1Keys, o2Keys);
    return o1Keys === o2Keys;    
}

console.log(sameStructure(obj1, obj2));