Skip to content

Instantly share code, notes, and snippets.

@Jontyy
Created August 22, 2012 11:07
Show Gist options
  • Save Jontyy/3424458 to your computer and use it in GitHub Desktop.
Save Jontyy/3424458 to your computer and use it in GitHub Desktop.
jQuery.reduce
//jQuery.reduce.js - https://github.com/jontyy - MIT licensed
jQuery.reduce = function(arr, func, initial){
initial = typeof initial === 'undefined' ? 0 : initial;
$.each(arr,function(i,v){
initial = func(initial,v,i);
});
return initial;
};
jQuery.fn.reduce = function(func,initial){
return jQuery.reduce(this,func,initial);
};
//usage
var sum = $.reduce([5,10,15],function(current,sum){
return current + sum;
});//30
var concat = $.reduce(['hello','world','universe'],function(state, word){
return state + word;
},''); //helloworlduniverse
//jQuery.reduce.js - https://github.com/jontyy - MIT licensed
jQuery.reduce=function(e,t,n){return n=typeof n=="undefined"?0:n,$.each(e,function(e,r){n=t(n,r,e)}),n},jQuery.fn.reduce=function(e,t){return jQuery.reduce(this,e,t)}
//jQuery.reduce.js - https://github.com/jontyy - MIT licensed
jQuery.reduce = function(arr, func, initial){
initial = typeof initial === 'undefined' ? 0 : initial;
for(var i in arr){if(arr.hasOwnProperty(i) && !isNaN(parseInt(i,10))){
initial = func(initial,arr[i],i);
}}
return initial;
};
jQuery.fn.reduce = function(func,initial){
return jQuery.reduce(this,func,initial);
};
//jQuery.reduce.js - https://github.com/jontyy - MIT licensed
jQuery.reduce=function(e,t,n){n=typeof n=="undefined"?0:n;for(var r in e)e.hasOwnProperty(r)&&!isNaN(parseInt(r,10))&&(n=t(n,e[r],r));return n},jQuery.fn.reduce=function(e,t){return jQuery.reduce(this,e,t)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment