Enumerate an Object's Properties in JavaScript

Description

The following code shows how to enumerate an Object's Properties.

Example


<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var myData = {<!-- w  w w.j  a  v  a 2 s  .  c o m-->
name : "JavaScript",
weather : "Good",
printMessages : function() {
document.writeln("Hello " + this.name + ". ");
document.writeln(this.name +" is " + this.weather + ".");
}
};

myData.printMessages();

for ( var prop in myData) {
document.writeln("Name: " + prop + " Value: " + myData[prop]);
}
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Enumerate an Object