Skip to content

Instantly share code, notes, and snippets.

@NeuTrix
Last active May 1, 2017 02:28
Show Gist options
  • Save NeuTrix/14d2c1b418539205a8b823f0fdc3f494 to your computer and use it in GitHub Desktop.
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
"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