Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created March 23, 2012 23:22
Show Gist options
  • Save sfoster/2176284 to your computer and use it in GitHub Desktop.
Save sfoster/2176284 to your computer and use it in GitHub Desktop.
a map function for arrays *or* objects
function map(obj, fn, thisArg){
// suggest you polyfill if you need Javascript < 1.6
var result = {};
if(obj instanceof Array){
result = obj.map(fn, thisArg || null);
} else {
Object.keys(obj).forEach(function(key){
result[key] = fn.call(thisArg || null, obj[key], key, obj);
});
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment