For those of you who are looking for solution without the _x in the object try this code. A slight modification of the above code (which is brilliant)
stringToObj = function(path,value,obj) {
var parts = path.split("."), part;
var last = parts.pop();
while(part = parts.shift()) {
if( typeof obj[part] != "object") obj[part] = {};
obj = obj[part]; // update "pointer"
}
obj[last] = value;
}
As bonus the above code will work if you want to update parts of an existing object :)
var obj = {a:{b:3}};
stringToObj("a.b",10,obj);
console.log(obj); //result : {a:{b:10}}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…