Skip to content

Instantly share code, notes, and snippets.

@kewogc
Created August 5, 2015 12:13
Show Gist options
  • Select an option

  • Save kewogc/670fc4a25b1ecd5bbcd6 to your computer and use it in GitHub Desktop.

Select an option

Save kewogc/670fc4a25b1ecd5bbcd6 to your computer and use it in GitHub Desktop.

Revisions

  1. kewogc created this gist Aug 5, 2015.
    15 changes: 15 additions & 0 deletions _.replaceKey.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    _.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}]}