Skip to content

Instantly share code, notes, and snippets.

@lovenick
Created December 30, 2014 20:18
Show Gist options
  • Select an option

  • Save lovenick/88364f33bb95472dfb82 to your computer and use it in GitHub Desktop.

Select an option

Save lovenick/88364f33bb95472dfb82 to your computer and use it in GitHub Desktop.
Underscore - Map
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