Created
December 10, 2018 20:13
-
-
Save et4891/59e474dec9dc8c81d35a7ebfffbee4f1 to your computer and use it in GitHub Desktop.
Set nested object properties using dot notation with value
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
/* | |
* Set nested object properties using dot notation with value | |
* */ | |
const setNested = (obj, path, val) => { | |
const keys = path.split('.'); | |
const lastKey = keys.pop(); | |
const lastObj = keys.reduce((obj, key) => | |
obj[key] = obj[key] || {}, | |
obj); | |
lastObj[lastKey] = val; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment