Created
August 22, 2012 11:07
-
-
Save Jontyy/3424458 to your computer and use it in GitHub Desktop.
jQuery.reduce
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.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 |
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.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)} |
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.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); | |
}; | |
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.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