Created
September 15, 2015 19:18
-
-
Save apiv/ddda6f60b100212906f0 to your computer and use it in GitHub Desktop.
Reduce function for an object
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
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