Friday, May 17, 2024
135
rated 0 times [  142] [ 7]  / answers: 1 / hits: 140218  / 11 Years ago, sat, september 21, 2013, 12:00:00

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?


More From » ecmascript-5

 Answers
26

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');

[#75534] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ednakarolinal

Total Points: 187
Total Questions: 106
Total Answers: 118

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;