Created
September 11, 2019 19:20
-
-
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.
This file contains hidden or 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
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