40 lines
1002 B
JavaScript
40 lines
1002 B
JavaScript
|
|
function Walk(obj)
|
|
{
|
|
const set = new Set();
|
|
function Step(obj, path="")
|
|
{
|
|
if(obj !== null && typeof obj === "object")
|
|
{
|
|
if(set.has(obj))
|
|
{
|
|
console.log("ive been to", obj, "quitting");
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
set.add(obj);
|
|
console.log("ive never seen", obj, "here are its fields:");
|
|
console.log("-----------------");
|
|
for(let key in obj)
|
|
{
|
|
const val = obj[key];
|
|
console.log(" >stepping into", path+"."+key);
|
|
Step(val, path+"."+key);
|
|
}
|
|
console.log("-----------------");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
console.log(obj, "is a leaf");
|
|
}
|
|
}
|
|
Step(obj);
|
|
}
|
|
|
|
|
|
const root = {hey:"1", deep:{basement:{closet:{item1:"one", item2:"two"}}}, arr:["a", "b"]};
|
|
root.circ = root;
|
|
Walk(root);
|