Created
June 6, 2015 23:08
-
-
Save codyromano/6409bebf832587c579ee to your computer and use it in GitHub Desktop.
Simple helper method for working with JSON
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
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