Skip to content

Instantly share code, notes, and snippets.

@calebdwilliams
Created August 7, 2019 17:46
Show Gist options
  • Save calebdwilliams/114d3a5d5960944994e1802a3e59ad25 to your computer and use it in GitHub Desktop.
Save calebdwilliams/114d3a5d5960944994e1802a3e59ad25 to your computer and use it in GitHub Desktop.
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