Skip to content

Instantly share code, notes, and snippets.

@inem
Forked from NV/Array.reduce.js
Created January 31, 2010 04:51
Show Gist options
  • Save inem/290910 to your computer and use it in GitHub Desktop.
Save inem/290910 to your computer and use it in GitHub Desktop.
var A_reduce = [].reduce;
Array.prototype.reduce = function reduce (callback, initialValue) {
var operator = callback;
return typeof operator == 'string' && /^[ +*/%-><!&|^?,.]/.test(operator)
? eval(this.join(operator))
: A_reduce.apply(this, arguments)
};
[1,2,3,4].reduce('+') // 10
[1,2,3,4].reduce('*') // 24
[2, 0, 1].reduce('&&') // 0
[2, 0, 1].reduce('||') // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment