Thursday 30 August 2018

JavaScript: Iterating over all properties of object

for..in loop is used to iterate over all the properties of object.

Syntax
for(property in object){
         var val = object[property];
}

iterate.html
<!DOCTYPE html>

<html>

<head>
    <title>iterate Object</title>
</head>

<body>
    <script type="text/javascript">
        var person1 = {
            firstName: "Hari Krishna",
            lastName: "Gurram",
            city: "Bangalore",
            area: "Marthalli"
        };

        for (property in person1) {
            document.write(property + " : " + person1[property] + "<br />");
        }
    </script>
</body>

</html>


Previous                                                 Next                                                 Home

No comments:

Post a Comment