assign sub-object to a variable in javascript - Javascript Object

Javascript examples for Object:Object Operation

Description

assign sub-object to a variable in javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width"> 
   </head> 
   <body> 
      <script>
var obj = {//from  w  ww  . j  a  v  a  2s . co m
    p1: {
        name: "name1",
        age: "age1"
    },
    p2: {
        name: "name2",
        age: "age2"
    }
};
for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
        console.log(obj[p].name);
    }
}
console.log(obj.p1.name === obj['p1']['name']);

      </script>  
   </body>
</html>

Related Tutorials