Skip to content

Instantly share code, notes, and snippets.

@qcom
Created January 16, 2014 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcom/8464730 to your computer and use it in GitHub Desktop.
Save qcom/8464730 to your computer and use it in GitHub Desktop.
An implementation of reduce modeled after the underscore.js api that does not default to Array.prototype.reduce.
function reduce(obj, iterator, memo, ctx) {
var keys = Object.keys(obj),
memo = memo || obj[keys[0]];
ctx = ctx || null;
for (var i = arguments[2] ? 0 : 1; i < keys.length; i++)
memo = iterator.call(ctx, memo, obj[keys[i]], keys[i], obj);
return memo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment