Last active
December 21, 2015 12:29
-
-
Save stephanschubert/6306212 to your computer and use it in GitHub Desktop.
The missing counterpart of jQuery.map()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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