Extend a JavaScript object by passing a string with the path and a value
问题 Is there an easy way to extend a JavaScript object by passing a string and a value? Basically I need something like this: myObject = {} var extendObj = function(obj, path, value){ } var path = "a.b.c", value = "ciao"; extendObj(myObject, path, value); console.log(myObject.a.b.c) //will print "ciao" 回答1: myObject = {}; var extendObj = function (obj, path, value) { var levels = path.split("."), i = 0; function createLevel(child) { var name = levels[i++]; if(typeof child[name] !== "undefined" &&