Created
August 7, 2019 17:46
-
-
Save calebdwilliams/114d3a5d5960944994e1802a3e59ad25 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 const updatePath = (obj, path, value) => { | |
const paths = path.split('.'); | |
const location = paths.pop(); | |
let pointer = obj; | |
for (let point in paths) { | |
const path = paths[point]; | |
if (path in pointer) { | |
pointer = pointer[path]; | |
} else { | |
throw new Error(`No property ${path} in ${pointer}`); | |
} | |
} | |
pointer[location] = value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment