Last active
May 1, 2017 02:28
-
-
Save NeuTrix/14d2c1b418539205a8b823f0fdc3f494 to your computer and use it in GitHub Desktop.
JavaScript. Iterating through an Object with a for...in loop : mini phone book
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
"use strict()"; | |
// make a phoneBook object | |
var phoneBook = {}; | |
// add some people objects | |
phoneBook.bob= {name:"Bob",age:35}; | |
phoneBook.greg= {name:"Greg",age:27}; | |
phoneBook.nancy= {name:"Nancy", age:65}; | |
//iterate over each item in the phoneBook | |
// `item` will be returned as a String so use [] method to acces properties | |
for(entry in phoneBook) { | |
var person = phoneBook[entry] | |
console.log("Name: " + person.name + " Age: "+ person.age ); | |
}; | |
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment