Created
March 23, 2012 23:22
-
-
Save sfoster/2176284 to your computer and use it in GitHub Desktop.
a map function for arrays *or* objects
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
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