Created
July 27, 2015 20:36
-
-
Save rajeshsegu/fdbec81612c631dfc459 to your computer and use it in GitHub Desktop.
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({ | |
/* | |
* 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); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: