Skip to content

Instantly share code, notes, and snippets.

@tbusser
Last active January 29, 2016 08:57
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 tbusser/4bd77a7d39659f48ffe1 to your computer and use it in GitHub Desktop.
Save tbusser/4bd77a7d39659f48ffe1 to your computer and use it in GitHub Desktop.
Iterate over object properties
/**
* 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