1

I can access the inner properties, but cant seem to figure out how to access the name of the set. For example, in the following code I can get the values of planet earth but I cant see to figure out how to display the name earth

var planet = {
  Earth: [
    'water',
    'rock'
  ]
};

alert(**output Key/Earth ** + " consists of " planet.Earth[0] + " & " + planet.Earth[1]);

I've tried this as well. I can get type and location but not the planet itself such as "Earth" or "Venus" to be displayed. I either get [undefined] or [object object]

var planets = {
    "Earth": {type: "Terrestrial ", location: "Milky Way"},
    "Venus": {type: "Terrestrial", location: "Milky Way"}
};

alert("Planet: " + planets[0] + " TYPE: " + planets.Earth.type + "LOCATION: " + planets.Earth.location);
3
  • 2
    Possible duplicate of Javascript get object key name Commented Aug 11, 2017 at 14:27
  • 2
    Object.keys(planets)[0] ?? Commented Aug 11, 2017 at 14:30
  • This totally worked! Object.keys(planets)[0] Cheers! Commented Aug 11, 2017 at 14:47

3 Answers 3

1

You can iterate through object keys using for(var key in obj) pattern, like this:

var planet = {
   Earth: [
     'water',
     'rock'
   ]
};
        
for(var key in planet) {
  console.log(key)
}

Or with your second example:

var planets = {
  "Earth": {type: "Terrestrial ", location: "Milky Way"},
  "Venus": {type: "Terrestrial", location: "Milky Way"}
};

for(var key in planets) {
  console.log(key);
}

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Commercial Suicide I found your response quite useful as I will need to iterate through them as sets. Cheers!
@loganpixel, nice to hear, that it helped you!
1

Thanks for the help and guidance from all respondents, each was beneficial in helping me to understand this problem. I selected the following from @Commercial Suicide as my answer as the code was succinct and enabled me a way to iterate through them. I can now continue to access my planets and their various properties.

In Regards to duplicate: I didn't see the answer on the other page as a duplicate right away. many of the top answers were about getting foo or fizz properties and not the key. I see now buried further down in those answers someone did provide the answer "ECMAscript edition 5 also offers you the neat methods Object.keys()" Javascript get object key name

Cheers!

var planets = {
  "Earth": {type: "Terrestrial ", location: "Milky Way"},
  "Venus": {type: "Terrestrial", location: "Milky Way"}
};


//expanded for loop //
for(var key in planets) {
console.log("PLANET: " + key + " TYPE: " + planets.Earth.type + "LOCATION: " + planets.Earth.location);
}

UPDATE!! Here is a fiddle that shows the above as well as getting values from the inner object.

var moviesNested = {
  Morag: {
    Movie: 'Guardians..Galaxy ',
    Hero: "Star Lord"
  },
  Tatooine: {
    Movie: 'Star Wars',
    Hero: "Skywalker"
  }
};

function showMovieData(movieSet) {

 for (var property in movieSet) {

  //outer property ------------------------------------
  console.log(property); 

  //inner as keys ---------------------------------
  for (var key in movieSet[property])

  console.log(key); //hero & movie
  console.log(movieSet[property].Movie);
  console.log(movieSet[property].Hero);

  var fullString = "<p>" + property + " <br /> " +
  movieSet[property].Movie + "<br /> " +
  movieSet[property].Hero;

  document.getElementById("outJS").innerHTML += fullString;
  //document.getElementById("content").innerHTML += "Loop: " + i + "<br>"; 
  }
}

showMovieData(moviesNested);

https://jsfiddle.net/carvingpixel/86rfvp8d/

Comments

0

You can use Object.enteries. It returns an array from object.

var planets = {
    "Earth": {type: "Terrestrial", location: "Milky Way"},
    "Venus": {type: "Terrestrial", location: "Milky Way"}
};
var arr = Object.entries(planets);
arr.forEach((obj) => {
console.log("Planet: " + obj[0] + " TYPE: " + obj[1].type + " LOCATION: " + obj[1].location);
});

You can also use Object.keys

var planets = {
    "Earth": {type: "Terrestrial", location: "Milky Way"},
    "Venus": {type: "Terrestrial", location: "Milky Way"}
};
Object.keys(planets).forEach((key) => {
console.log("Planet: " + key + " TYPE: " + planets[key].type + " LOCATION: " + planets[key].location);
});

2 Comments

Thank you, Hassan Imam. The snippet worked great here on the site but in my IDE I get highlighted warning and nothing happens. "Expression statement is not assignment or call". I checked the link you provided regarding Object.entries as well but had no success with it. "Unresolved function or method entries()" trying to use that page example of console.log(Object.entries(obj));
Yes, it's an experimental feature hasn't been standardised yet. I have updated the solutions using Object.keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.