Last active
December 15, 2017 12:45
-
-
Save iegik/3643b06b919bed078f9d490e94bdbc22 to your computer and use it in GitHub Desktop.
Assign value to the context by key path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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