Updated query. Thanks to @WiktorZychla for sparking my Monday morning thoughts on recursion. The revised code is functioning correctly now.
Assuming I have a dummy object structured like this:
const dummy = {
a: 1,
b: 2,
c: {
d: 3,
e: {
f: 4
}
},
g: 5
};
I can traverse through it using the following function:
const xavier = (value, s) => {
for (const key in value) {
if (value.hasOwnProperty(key)) {
if (typeof value[key] === 'object' && value[key] !== null) {
xavier(value[key], s + '.' + key);
} else {
console.log(s + '.' + key + ' ' + value[key]);
}
}
}
};
The output of this function is as follows:
.a 1
.b 2
.c.d 3
.c.e.f 4
.g 5