Skip to content

Instantly share code, notes, and snippets.

@sungwoncho
Created July 1, 2016 05:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sungwoncho/8caf7f3d9e56741c0631cda134e01832 to your computer and use it in GitHub Desktop.
Save sungwoncho/8caf7f3d9e56741c0631cda134e01832 to your computer and use it in GitHub Desktop.
// Given path and value, get an object
// e.g. getObj('a.b.c', 1)
// { a: { b: { c: 1 } } }
function getObj(path, val) {
const keys = path.split('.');
if (keys.length === 1) {
const current = keys[0];
return {
[current]: val
};
} else {
const current = keys.shift();
const obj = getSourceObj(keys.join('.'), val);
return {
[current]: obj
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment