Traversing an Object's Properties

for CSCI E3, Harvard University author(s): Larry Bouthillier

by DustyWhite

HTML

<h3>Traversing an Object's Properties</h3>
<p>We use the <br/>
    <blockquote><code>for (property in object){}</code></blockquote>
    <br/>
 loop to iterate over all of our object's properties. </p>
<b>Open the console to see the output of this code.</b>

JavaScript

// here's our object
var myAddrBookEntry = {
    "fname": "Larry",
    "lname": "Bouthillier",
    "addr": "Rhode Island",
    "email": "[email protected]",
    "person-role" : "instructor",
	"getFullName":function(){
			return this.fname + " " + this.lname;
	}
}

for (var key in myAddrBookEntry){
   console.log(key + ":" + myAddrBookEntry[key]);
}