Underscore mixin with common iterator functions adapted to work with objects and maintain key/val pairs.
_.mixin({ | |
// ### _.objMap | |
// _.map for objects, keeps key/value associations | |
objMap: function (input, mapper, context) { | |
return _.reduce(input, function (obj, v, k) { | |
obj[k] = mapper.call(context, v, k, input); | |
return obj; | |
}, {}, context); | |
}, | |
// ### _.objFilter | |
// _.filter for objects, keeps key/value associations | |
// but only includes the properties that pass test(). | |
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); | |
}, | |
// ### _.objReject | |
// | |
// _.reject for objects, keeps key/value associations | |
// but does not include the properties that pass test(). | |
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); | |
} | |
}); |
This comment has been minimized.
This comment has been minimized.
I added filter and reject, as per this issue: jashkenas/underscore#359. And also because I needed it for the Drupal Backbone module. |
This comment has been minimized.
This comment has been minimized.
Nice |
This comment has been minimized.
This comment has been minimized.
Very useful, thank you |
This comment has been minimized.
This comment has been minimized.
+1 +1 +1 |
This comment has been minimized.
This comment has been minimized.
thanks for this |
This comment has been minimized.
This comment has been minimized.
Thanks dude. |
This comment has been minimized.
This comment has been minimized.
Hey, I'd like to package this. Can you add a license? |
This comment has been minimized.
This comment has been minimized.
Hey, just following up. Would you mind adding a license to this gist? Underscore uses MIT, or you could put it in the public domain. Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
See jashkenas/underscore#220