Retrieving key of a JavaScript object using its index - Javascript Object

Javascript examples for Object:Object Keys

Description

Retrieving key of a JavaScript object using its index

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  w  ww .jav  a 2  s .  co  m
var obj = {
    "set1": [1, 2, 3,5,6,7,8,9],
    "set2": [4, 5, 6, 7, 8],
    "set3": [9, 10, 11, 12]
};
var sets = [];
for ( var o in obj ) {
  if (obj.hasOwnProperty(o)) {
     sets.push( o );
  };
};

console.log(sets[1]);

var sets = Object.keys(obj);
console.log(sets[1]);

    }

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

Related Tutorials