Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Created July 10, 2015 22:40
Show Gist options
  • Save postpostscript/cdf523553df88c472605 to your computer and use it in GitHub Desktop.
Save postpostscript/cdf523553df88c472605 to your computer and use it in GitHub Desktop.
Object mapping and filtering
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