Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.