Created
July 11, 2012 15:36
-
-
Save boh1996/3091206 to your computer and use it in GitHub Desktop.
Set object by path value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author devman | |
*/ | |
function setObjectPathValue(source, path, value) { | |
var parts = path.split('.'), len = parts.length, target = source; | |
for (var i = 0, part; i < len - 1; i++) { | |
part = parts[i]; | |
target = target[part] == undefined ? (target[part] = {}) : target[part]; | |
} | |
target[parts[len - 1]] = value; | |
return target; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment