Created
January 10, 2019 11:19
-
-
Save t-davies/2bd97f5dbe488d0f64d4733b179fa650 to your computer and use it in GitHub Desktop.
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
export function getNestedValue(key, object) { | |
return key.split('.').reduce((previous, current) => { | |
return previous[current]; | |
}, object); | |
} | |
export function setNestedValue(key, value, object) { | |
const compound = key.split('.'); | |
return compound.reduce((previous, current, index) => { | |
if (index === compound.length - 1) { | |
previous[current] = value; | |
return object; | |
} | |
return previous[current]; | |
}, object); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment