TLDR Edit: I was confusing object destructuring w/ arrays and spread syntax. Below edit does the trick to recursively unpack the nested object.
let nextLevel = Object.values(obj)
return goingDeep(...nextLevel)
Following is the original question, which I'll leave up in case another noob such as myself runs into this, help save them from the downvotes ;p
Attempt to destructure a nested object recursively returns undefined. Putting that aside simply trying to destructure the same object into different variables returns undefined.
Keeping it simple, just assuming a single key:value (object) per layer so no need to iterate.
const container = {container1: {container2 : {container3: {container4: 'core'}}}}
Putting recursion aside for the moment the following results with simply two different destructuring assignments...
const {level1} = container
console.log(level1) // => container1: {container2:{ etc
const {level1_different} = container
console.log(level1_different) // => undefined
this is what I attempted with the recursion
const goingDeep = (obj) => {
if (obj.hasOwnProperty('container4')){
obj.container4 = 'found'
return obj
} else {
// let {nextLevel} = obj /no good
// return goingDeep(nextLevel) /no good
let nextLevel = Object.values(obj)
return goingDeep(...nextLevel)
}
}
originally had the destructuring at the parameter goingDeep = ({obj})
which I have used successfully for arrays so clearly I'm misunderstanding something(s) fundamental to destructuring objects