Skip to content
Advertisement

dynamicly nested object from another object

trying to figure out how to dynamicly create a new nested object from this one:

object1 = {
    DataStore : false,
    Header: false,
    Footer : false,
    Sidebar : false,
    Main : false,
    }

to nested one like this:

const registerComponentsLocal = {
    'DataStore': {
        'debug': false
    },
    'Header': {
        'debug': false
    },
    'Footer': {
        'debug': false
    },
    'Sidebar': {
        'debug': false
    },
    'Main': {
        'debug': false
    },
}

keys and values have to by dynamic. Only important thing is a structure of the final object. Any ideas would be greatly appricieated.

Advertisement

Answer

To create a new instance (i.e preserve the old one)

let originalObject = {
    DataStore : false,
    Header: false,
    Footer : false,
    Sidebar : false,
    Main : false,
    }

let newObject = Object.assign({}, originalObject) // Copies the original object
Object.entries(newObject).forEach(([key, value]) => newObject[key] = {debug: value})


Advertisement