Skip to content

Instantly share code, notes, and snippets.

@Trylobot
Forked from eethann/_.objMapFunctions.js
Last active December 22, 2015 05:49
Show Gist options
  • Save Trylobot/6426442 to your computer and use it in GitHub Desktop.
Save Trylobot/6426442 to your computer and use it in GitHub Desktop.
// keeps key/value associations
_.mixin({
// similar to _.map
objMap: function( input, mapper, context ) {
return _.reduce( input, function (obj, v, k) {
obj[k] = mapper.call( context, v, k, input );
return obj;
}, {}, context);
},
// similar to _.filter
objFilter: function( input, test, context ) {
return _.reduce( input, function (obj, v, k) {
if( test.call( context, v, k, input )) {
obj[k] = v;
}
return obj;
}, {}, context);
},
// similar to _.reject
objReject: function( input, test, context ) {
return _.reduce( input, function (obj, v, k) {
if( !test.call( context, v, k, input )) {
obj[k] = v;
}
return obj;
}, {}, context);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment