Given an object shaped like the following, (which can have unknown number of nested properties)
const theme = {
fonts: {
primary: 'Arial',
secondary: 'Helvetica'
},
colors: {
primary: 'green',
secondary: 'red',
},
margin: {
small: '0.5rem',
medium: '1rem',
large: '1.5rem'
}
}
Im trying to achieve the following:
- Loop through recursively until i hit a value that isn't an object
- When i hit this value i would like to have access to all the keys that lead up to it, along with the end value.
Something like the following:
['fonts', 'primary'], 'Arial'
['fonts', 'secondary'] 'Helvetica'
['colors', 'primary'] 'green'
etc.
I have tried various different attempts, but the bit that is stumping me is how do i keep track of the keys, and reset them when the original loop is called again?