Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sets the values of a javascript object or array using a chain of colon separated keys.
function set(obj, keyChain, value) {
var keys = keyChain.split('.');
var key = keys.shift();
while(keys.length) {
obj[key] = obj[key] || (isFinite(keys[0]) ? [] : {});
obj = obj[key];
key = keys.shift();
}
obj[key] = value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment