I am facing a difficulty to loop through an object (the object also contains array in different layers) with at least 6 layers and show key/value pairs. , I tried to use For...In statement or recursion to loop through it. Sadly, it was not successful. Could anyone kindly provide the solution for me please, thank you very much.
- Recursive Function
function keyValuePairFunc(obj) {
for (const [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`)
if (typeof value === "object") {
for (const [key, val] of Object.entries(value)) {
console.log(`${key}: ${val}`)
}
}
}
}
keyValuePairFunc(loadedData);