Skip to content

Instantly share code, notes, and snippets.

@apiv
Created September 15, 2015 19:18
Show Gist options
  • Save apiv/ddda6f60b100212906f0 to your computer and use it in GitHub Desktop.
Save apiv/ddda6f60b100212906f0 to your computer and use it in GitHub Desktop.
Reduce function for an object
function reduceObject(obj, fn, init) {
return Object.keys(obj).reduce(function (accum, key) {
return fn(accum, obj[key], key, obj);
}, init);
}
// usage
var obj = {
a: [1,2,3],
b: [4,5,6],
c: [7,7,7]
};
reduceObj(obj, function(accum, val, key, object) {
return accum.concat(val);
}, []);
// [1,2,3,4,5,6,7,7,7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment