Skip to content

Instantly share code, notes, and snippets.

@et4891
Created December 10, 2018 20:13
Show Gist options
  • Save et4891/59e474dec9dc8c81d35a7ebfffbee4f1 to your computer and use it in GitHub Desktop.
Save et4891/59e474dec9dc8c81d35a7ebfffbee4f1 to your computer and use it in GitHub Desktop.
Set nested object properties using dot notation with value
/*
* 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