Skip to content

Instantly share code, notes, and snippets.

@t-davies
Created January 10, 2019 11:19
Show Gist options
  • Save t-davies/2bd97f5dbe488d0f64d4733b179fa650 to your computer and use it in GitHub Desktop.
Save t-davies/2bd97f5dbe488d0f64d4733b179fa650 to your computer and use it in GitHub Desktop.
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