Skip to content

Instantly share code, notes, and snippets.

@codyromano
Created June 6, 2015 23:08
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 codyromano/6409bebf832587c579ee to your computer and use it in GitHub Desktop.
Save codyromano/6409bebf832587c579ee to your computer and use it in GitHub Desktop.
Simple helper method for working with JSON
var foods = {
'italian' : 'pizza',
'mexican' : 'burrito',
'japanese' : 'sushi'
};
// Normal way of iterating
for (var category in foods) {
console.log(foods[category], ' is a type of ', category, ' food.');
}
// Simple helper function for iterating over each key/value pair
function forEachInObj (object, callback) {
for (var key in object) {
callback(key, object[key]);
}
}
// This is a little simplier to read and write
forEachInObj(foods, function (category, dish) {
console.log(dish, ' is a type of ', category, 'food.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment