Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active December 15, 2017 12:45
Show Gist options
  • Save iegik/3643b06b919bed078f9d490e94bdbc22 to your computer and use it in GitHub Desktop.
Save iegik/3643b06b919bed078f9d490e94bdbc22 to your computer and use it in GitHub Desktop.
Assign value to the context by key path
/**
* Assign value to the context by key path
* @param {array} path ['foo', 'bar']
* @param {mixed} value
* @returns {*} result {foo:{bar: value}}
*/
function _define (path, value){
return path.reduce((x, y, i) => {
return x[y] = i === path.length - 1 && typeof value !== 'undefined' ? value : x[y] || {};
}, this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment