Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

Rhys Kearns
Rhys Kearns
4,976 Points

Getting object name not value

Is there a way to get the object name and not the value?

var students = [
  {
    name: "Rhys",
    track: "Fullstack Javascript",
    achievments: 183,
    points: 6732
  },
  {
    name: "Ryan",
    track: "MySQL",
    achievments: 34,
    points: 1092
  },
  {
    name: "James",
    track: "Fullstack Web Developer",
    achievments: 431,
    points: 29432
  }
];

for (var prop in students) {
  var list = "<h2>Name: " + students[prop].name + "</h2>";
  list += "<p>Track: " + students[prop].track;
  list += "<br>Achievements: " + students[prop].achievments;
  list += "<br>Points: " + students[prop].points + "</p>";
  print(list);
}

function print(message) {
  var div = document.getElementById('output');
  div.innerHTML += message;
}

Is there a way for me to not hard code "Achievements:", "Name:" and be able to get the property name directly from the array of objects?

1 Answer