0

This seems rather basic but I am having an impossible time trying to figure this out... How do I access the property value of a property that is in an object within an object?

Ex:

var myObj = {numberOne: {type1: "one", type2:"two"}};

In my case, numberOne is constantly changing as are the property values of the properties within it ("one", "two"). What does not change are the property names themselves (type1, type2). I am looking for a way to access the property value of type1 ("one") as it is the only value I'm interested in working with.

My thought is that I need to create a key for numberOne and use a for... in loop but I'm not pulling up the correct results. I can only get as far as extracting the property names.

var myObj = {numberOne: {type1: "one", type2: "two"}};
var key = Object.keys(myObj)[0];

for (var prop in myObj[key]){
  console.log(prop);
}
macks
  • 5
  • 2
  • possible duplicate of [How do I access properties of a javascript object if I don't know the names?](http://stackoverflow.com/questions/675231/how-do-i-access-properties-of-a-javascript-object-if-i-dont-know-the-names) – Matt Burland May 19 '15 at 19:47
  • 2
    `console.log(myObj[key][prop]);` – Matt Burland May 19 '15 at 19:48
  • 1
    Also, your object literal syntax is wrong. Should be: `var myObj = {numberOne: {type1 : "one", type2 : "two"}};` There are no `=` in an object literal – Matt Burland May 19 '15 at 19:50
  • Thanks for the help. And yes, realized I was using = instead of : and fixed that. – macks May 19 '15 at 19:57

0 Answers0