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

Skipping a property when iterating through an object using a FOR/IN loop

In attempting the Objects code challenge, I came across a small hiccup;
Is there any way to skip a property when iterating through an object using a FOR/IN loop?
Specifically, I want to skip the "name" key.
Here is my codes output:

Student: Nijad  

    name: Nijad  
    track: PHP  
    achv: 1  
    points: 2  

Student: Yumi  

    name: Yumi  
    track: JS  
    achv: 3  
    points: 4  

Student: Nina  

    name: Nina  
    track: PHP  
    achv: 5  
    points: 6  

Student: Mike   

    name: Mike  
    track: PHP  
    achv: 7  
    points: 8  

Student: Chucky  

    name: Chucky  
    track: PHP  
    achv: 9  
    points: 10  

Here is my code:

var students = [   
    { name: "Nijad",   
      track: "PHP",   
      achv: 1,   
      points: 2 } ,       
    { name: "Yumi",   
      track: "JS",   
      achv: 3,   
      points: 4 } ,   
    { name: "Nina",   
      track: "PHP",   
      achv: 5,   
      points: 6 } ,   
    { name: "Mike",   
      track: "PHP",   
      achv: 7,   
      points: 8 } ,   
    { name: "Chucky",   
      track: "PHP",   
      achv: 9,   
      points: 10 } ,   
] ;    

var student;  
var message = ' ' ;  

for (var i = 0; i < students.length; i++ ) {  
    student = students[i] ;  
    message += "<h2>Student: " + student.name + "</h2><ul>" ;  
/* using a FOR loop to create a single dimension array called student from the students multidimensional array */  

    for ( var key in student ) {  
        message += "<li>" + key + ": " + student[key] + "</li>" ;  
    }  
    /* using a FOR/IN loop to iterate through each property of the student array    */  
    message += "</ul>" ;  

}  

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

print( message ) ;  

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Nijad Kifayeh

Yes! Just use a conditional statement inside the for...in loop to check for the key name:

for ( var key in student ) {  
    if (key !== 'name') {
         message += "<li>" + key + ": " + student[key] + "</li>" ;  
    }
} 

ah..so simple! Thanks Dave!!

Having said that....I can't figure out what's wrong with the code now, can you take a look please?

var students = [ 
    { name: "Nijad", 
      track: "PHP", 
      achv: 1, 
      points: 2 } ,     
    { name: "Yumi", 
      track: "JS", 
      achv: 3, 
      points: 4 } , 
    { name: "Nina", 
      track: "PHP", 
      achv: 5, 
      points: 6 } , 
    { name: "Mike", 
      track: "PHP", 
      achv: 7, 
      points: 8 } , 
    { name: "Chucky", 
      track: "PHP", 
      achv: 9, 
      points: 10 } , 
] ;  
/* an array of objects where each object is a student record */

var query ;
var student ;
var record ;
var message = ' ' ;

while( true ) {

    query = prompt( 'Enter the name of the record you would like to search for or type "quit" to exit' ) ;

    if( query === null || query.toLowerCase() === 'quit' ) {
        break ;
    }
    /* the search query prompt; the users response will be saved in the query variable. the loop condition will always evaluate as true, and therfore the prompt will remain, until the user types "quit" (note that quit is in quotes since it is a string) or clicks cancel; if the user clicks cancel, the prompt window returns a value of null, which is why we need to specify the case where query === null (null does not need to be in quotes since it is not a string) */

    for( var i = 0; i < students.length; i++ ) {
    student = students[i] ;
/* using a FOR loop to create a single dimensional array called student from the students multidimensional array */  

        if( query.toLowerCase() === student.name.toLowerCase() ) {
            record = students[i] ;
            message += "<h2>Student: " + students[i].name + "</h2><ul>"  ;
        /* if the query response === the value of the name key in the student (single dimensional) array, then the record variable should hold the object at index i of the students array */

            for( var key in student ) {
                if (key !== 'name') {
                    message += "<li>" + key + ": " + students[i][key] + "</li>" ;
                }
            /* using a FOR/IN loop to iterate through each property of the student array; the conditional statement allows us to not iterate the name key  */    
            }
            message += "</ul>" ;
        }
    }

}
Dave McFarland
Dave McFarland
Treehouse Teacher

You don't have the print() function in this code, and you don't ever call a function to print to the page.