Skip to content

Instantly share code, notes, and snippets.

@abruzzihraig
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abruzzihraig/ffeea25203d6a86758b3 to your computer and use it in GitHub Desktop.
Save abruzzihraig/ffeea25203d6a86758b3 to your computer and use it in GitHub Desktop.
This is a patch that supports reduce function for jQuery.
$.reduce = function(arr, reduce_cb, prev) {
arr = arr.toArray();
if (Array.prototype.reduce) {
return Array.prototype.reduce.call(arr, reduce_cb, prev);
}
$.each(arr, function(i, cur) {
result = reduce_cb.call(null, prev, cur, i, arr);
prev = cur;
});
return result;
};
$.fn.reduce = function (cb, prev) {
return $.reduce(this, function(prev, cur, i, arr) {
return cb.call(null, prev, cur, i, arr);
}, prev);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment