Skip to content

Instantly share code, notes, and snippets.

@rajeshsegu
Created July 27, 2015 20:36
Show Gist options
  • Select an option

  • Save rajeshsegu/fdbec81612c631dfc459 to your computer and use it in GitHub Desktop.

Select an option

Save rajeshsegu/fdbec81612c631dfc459 to your computer and use it in GitHub Desktop.

Revisions

  1. rajeshsegu created this gist Jul 27, 2015.
    21 changes: 21 additions & 0 deletions _.walk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    _.mixin({

    /*
    * Walk trough every <key, value> in a given object or array
    * Usage:
    * var obj = {a: 1, b: 2, c: {d: 3, e: 4}}
    * _.walk(obj, function(value, key, obj){
    * console.log(value);
    * });
    * //Output: 1, 2, 3, 4
    */
    walk: function(obj, iterator, context){
    _.each(obj, function(value, key){
    if(_.isObject(value)){
    _.walk(value, iterator, context);
    }else{
    iterator.call(context, obj[key], key, obj);
    }
    });
    }
    });