Created
December 30, 2014 20:18
-
-
Save lovenick/88364f33bb95472dfb82 to your computer and use it in GitHub Desktop.
Underscore - Map
This file contains hidden or 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
| var map = function(obj, iterator) { | |
| obj = obj || {}; | |
| iterator = iterator || null; | |
| var results = []; | |
| if(!iterator) return; | |
| if(obj instanceof Array) { | |
| for(var i = 0; i < obj.length; i++) { | |
| results.push(iterator(i, obj[i])); | |
| } | |
| } else { | |
| for(var key in obj) { | |
| results.push(iterator(key, obj[key])); | |
| } | |
| } | |
| return results; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment