Created
August 5, 2015 12:13
-
-
Save kewogc/670fc4a25b1ecd5bbcd6 to your computer and use it in GitHub Desktop.
underscore.js mixin for set key of nested object
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
_.mixin | |
replaceKey: (obj, key, value) -> | |
_obj = _(obj) | |
_obj.each (v, k) -> | |
if k == key | |
obj[k] = value | |
else if obj[k] == Object(obj[k]) | |
replaceKey obj[k], key, value | |
return | |
_obj | |
# usage | |
# obj = {a: 1 , b: [{a: 2}]} | |
# _.replaceKey(obj, 'a', 3) | |
#obj-> a: 3 , b: [{a: 3}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment