Last active
January 29, 2016 08:57
-
-
Save tbusser/4bd77a7d39659f48ffe1 to your computer and use it in GitHub Desktop.
Iterate over object properties
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
/** | |
* Iterates over the properties of an object and calls a callback function when the | |
* key belongs to the object itself and not to its prototype chain. | |
* | |
* The iterate method will iterate over all properties but can be stopped by | |
* returning true from the callback method. | |
* | |
* @param {Object} source The object whose properties should be iterated over | |
* @param {Function} callback The function to be called for each property defined | |
* on the source object itself and not its prototype | |
* chain. | |
* The callback will be called with two params. The | |
* first param is the current property name (key) and | |
* the second param is the value of the current property. | |
*/ | |
function iterate(source, callback) { | |
for(var key in source) { | |
if (source.hasOwnProperty(key)) { | |
if (callback(key, source[key])) { | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment