Created
July 10, 2015 22:40
-
-
Save postpostscript/cdf523553df88c472605 to your computer and use it in GitHub Desktop.
Object mapping and filtering
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
var MapObj = function(obj, mapFn) { | |
var key, result, val; | |
if (obj.length && obj.join) { | |
return obj.map(mapFn); | |
} | |
result = {}; | |
for (key in obj) { | |
val = obj[key]; | |
result[key] = mapFn(val, key); | |
} | |
return result; | |
}; | |
var FilterObj = function(obj, filterFn) { | |
var key, result, val; | |
if (obj.length && obj.join) { | |
return obj.filter(filterFn); | |
} | |
result = {}; | |
for (key in obj) { | |
val = obj[key]; | |
if (filterFn(val, key)) { | |
result[key] = val; | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment