Created
January 20, 2015 01:17
-
-
Save theverything/f8a52c42d0c9cd8b7afc to your computer and use it in GitHub Desktop.
object edit
This file contains 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
function edit(obj, key, value) { | |
if (!key) return obj; | |
var keys = key.split('.'); | |
return keys.reduce(function (prev, next, i) { | |
if (keys.length === i + 1 && value !== undefined) { | |
prev[next] = value; | |
return obj; | |
} else { | |
return prev[next]; | |
} | |
}, obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment