Skip to content

Instantly share code, notes, and snippets.

@kevinkub
Created September 11, 2019 19:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinkub/869af264f00508594945193fc04e0334 to your computer and use it in GitHub Desktop.
Save kevinkub/869af264f00508594945193fc04e0334 to your computer and use it in GitHub Desktop.
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