setDottedPath.js 311 B

12345678910111213141516
  1. 'use strict';
  2. module.exports = function setDottedPath(obj, path, val) {
  3. const parts = path.split('.');
  4. let cur = obj;
  5. for (const part of parts.slice(0, -1)) {
  6. if (cur[part] == null) {
  7. cur[part] = {};
  8. }
  9. cur = cur[part];
  10. }
  11. const last = parts[parts.length - 1];
  12. cur[last] = val;
  13. };