Search code examples
javascriptecmascript-5

Dynamically set property of nested object


I have an object that could be any number of levels deep and could have any existing properties. For example:

var obj = {
    db: {
        mongodb: {
            host: 'localhost'
        }
    }
};

On that I would like to set (or overwrite) properties like so:

set('db.mongodb.user', 'root');
// or:
set('foo.bar', 'baz');

Where the property string can have any depth, and the value can be any type/thing.
Objects and arrays as values don't need to be merged, should the property key already exist.

Previous example would produce following object:

var obj = {
    db: {
        mongodb: {
            host: 'localhost',
            user: 'root'
        }
    },
    foo: {
        bar: baz
    }
};

How can I realize such a function?


Solution

  • This function, using the arguments you specified, should add/update the data in the obj container. Note that you need to keep track of which elements in obj schema are containers and which are values (strings, ints, etc.) otherwise you will start throwing exceptions.

    obj = {};  // global object
    
    function set(path, value) {
        var schema = obj;  // a moving reference to internal objects within obj
        var pList = path.split('.');
        var len = pList.length;
        for(var i = 0; i < len-1; i++) {
            var elem = pList[i];
            if( !schema[elem] ) schema[elem] = {}
            schema = schema[elem];
        }
    
        schema[pList[len-1]] = value;
    }
    
    set('mongo.db.user', 'root');