Guest User

Untitled

a guest
Dec 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.30 KB | None | 0 0
  1. /*
  2. * Set nested object properties using dot notation with value
  3. * */
  4.  
  5. const setNested = (obj, path, val) => {
  6. const keys = path.split('.');
  7. const lastKey = keys.pop();
  8. const lastObj = keys.reduce((obj, key) =>
  9. obj[key] = obj[key] || {},
  10. obj);
  11. lastObj[lastKey] = val;
  12. };
Add Comment
Please, Sign In to add comment