Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
Last active December 21, 2015 12:29
Show Gist options
  • Save stephanschubert/6306212 to your computer and use it in GitHub Desktop.
Save stephanschubert/6306212 to your computer and use it in GitHub Desktop.
The missing counterpart of jQuery.map()
/* jQuery core team does not want to add 'reduce' -
* Details: http://dev.jquery.com/ticket/1886
*/
(function($) {
$.reduce = function(array, callback, initialValue) {
if (Array.prototype.reduce) {
return Array.prototype.reduce.call(array, callback, initialValue);
}
$.each(array, function(index, elem) {
initialValue = callback.call(null, initialValue, elem, index, array);
});
return initialValue;
};
$.fn.extend({
reduce: function(callback, initialValue) {
return $.reduce(this, callback, initialValue);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment