Created
December 17, 2013 17:43
-
-
Save gordonbrander/8009331 to your computer and use it in GitHub Desktop.
Enumerate object's own keys
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
// Enumerate over an object's own keys/values, accumulating a value. | |
// `initial` defines the initial value for the accumulation. Reference is an | |
// optional additional argument that make a common case -- comparing 2 | |
// objects -- easy and more efficient. | |
function enumerate(object, next, initial, reference) { | |
var accumulated = initial; | |
for (var key in object) | |
// Test for own keys with `hasOwnProperty` instead of `Object.keys` to | |
// avoid garbage creation. | |
// | |
// @TODO need to test this "optimization". | |
if(object.hasOwnProperty(key)) accumulated = next(accumulated, key, object[key], object, reference); | |
return accumulated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment