Try this testObject. You may want to do something to test for recursion, e.g. an array or map that tracks objects that you have already seen.
function unpackObject(obj, tabs = '') {
const props = Object.getOwnPropertyNames(obj);
let v;
props.forEach(p => {
v = obj[p];
//v = Object.getOwnPropertyDescriptor(obj, p).value; | side question: is it better?
if (v instanceof Object) {
console.log(`${tabs}${p} ${Object.prototype.toString.call(v)}`);
if (v instanceof Array)
v.forEach((x, i) => console.log(`${tabs}\t[${i}]: ${x}`));
else
unpackObject(v, `${tabs}\t`);
} else
console.log(`${tabs}${p}: ${v}`);
});
}
// Testing code:
let testObject = {
name: 'Miguel',
surname: 'Avila',
age: undefined, //LOL
marital_status: 'Single',
hobbies: ['Write Code', 'Watch YT Videos', 'etc. idk'],
contact: {
phones: ['xxxxxxxxxx', 'xxxxxxxxxx'],
address: 'unknown'
}
};
testObject.object = testObject;
unpackObject(testObject);
This actually works better than I expected. It eventually crashes and displays with an error message.