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 Student Record Search Challenge Solution

Tyler Durden
Tyler Durden
2,406 Points

Is there a way to use the for-in loop to get all the property names and values?

I understand using the while loop and if statement for check for when the prompt variables is equal to .name in the array.

but say there was like 100 properties of each object , and instead of building up the string and having to type that all out, is there a way to just use the FOR LOOP to scan through the array students[i].name, and then in the IF STATEMENT, if it is === to the name the user inputted via the prompt, can you not nest a FOR IN loop inside the for loop to then "spit out" or document.getElementByID('div name here').innerHTML all the property names and values of the Object? that way you don't have to type out 100 different properities in the HTML string ? (if you had an object with that much information I mean).

I tried to do this, but was getting "Object object" outputs , like it I couldn't access the real property values/string/int values.

Is the FOR IN loop only used for console.log then?

3 Answers

Steven Parker
Steven Parker
230,995 Points

It seems like this is done in one of the courses, but since I don't recall which here's a little function that will list out the contents of an object into a specified page element:

function showProperties(object, divID) {
  var html = "object properties:<ul>";
  for (property in object) {
    html += `<li>${property}: ${object[property]}</li>`;
  }
  html += "</ul>";
  document.getElementById(divID).innerHTML = html;
}
Tyler Durden
Tyler Durden
2,406 Points

and if you have multiple objects inside an array, you can first loop through the array then nest that for-in loop inside to get the properties of each object inside the array without having to explicitly use the object[property].name, etc... notation?

Steven Parker
Steven Parker
230,995 Points

Sure, but you'd probably want to modify the function so it adds to the page element instead of overwriting it.

Tyler Durden
Tyler Durden
2,406 Points

when I loop through that using the for-in loop, I get :

undefined0: [object Object]1: [object Object]2: [object Object]3: [object Object]4: [object Object]

as the output (document.getElementById('output').innerHTML method)for an Array I made containing 5 objects (each objects contains 4 properties).

I have not been able to get it to print/document.write the contents of the object via a loop to the page.

In the course video, he only showed the for-in looping through the objects with CONSOLE.LOG, he never actually printed the contents of the objects via a loop to the page.

Steven Parker
Steven Parker
230,995 Points

It doesn't look like you're calling "showProperties" for each object here. To completely analyze the issue, you'll need to show the whole code (all JS and HTML). Or better yet, make a snapshot of your workspace and post the link to it here.