Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Looping through an Array of objects

Hello there,

I am trying loop through an array of object. My code works fine but can be optimized:

var students = [

  {name: "Jack", track: "iOS", achievement: 5},
  {name: "Alex", track: "Javascript", achievement: 7},
  {name: "Robert", track: "Python", achievement: 0},
  {name: "Jill", track: "Ruby", achievement: 15},
 {name: "Angus", track: "C++", achievement: 3}
]

var input = prompt ("Give me a name, track or Achievement")

for (var i = 0; i < students.length; i++) { 

if ( input === students[i].name || input === students[i].track || input === students[i].achievement) { 

alert(input)
} 
  }

The scope of this code is to return data if there's a match in the system or database. As you can see in my IF statement I am declaring 3 arguments. What If my object has 200 properties like "address", "score", "color", etc.? It doesn't seem logical to me to declare all of them in an IF statement.

What I am trying to do is something like:

if ( input matches any of the properties of the object) {

alert(object property)

}

Thanks!

4 Answers

I guess it depends on what you are logging. If you log student just after student = students[i] inside the loop you should see an entry in the console for each student. If you log student after the loops you should see one entry - the last student. Maybe I don't understand what you are saying.

student is a variable for the individual objects in students. So for the first iteration student is:

{name: "Jack", track: "iOS", achievement: 5}

Then there is a second loop for each key of the current student:

for (var k in student)

k is the key (in this case name, track, achievement)

student[k] is the value for that key ("Jack", "iOS", 5)

A comparison is made between the value and input and if there is a match an alert is displayed.

The code then continues with the next student.

Note: I'd like to change my input comparison to use == instead of ===.

if (student[k] == input)

Currently with the type being checked, inputting one of the numeric values does not result in an alert because input is a string.

There are probably other (better?) ways to do this but this is what I came up with:

var students = [

  {name: "Jack", track: "iOS", achievement: 5},
  {name: "Alex", track: "Javascript", achievement: 7},
  {name: "Robert", track: "Python", achievement: 0},
  {name: "Jill", track: "Ruby", achievement: 15},
 {name: "Angus", track: "C++", achievement: 3}
]

var input = prompt ("Give me a name, track or Achievement")

for (var i = 0; i < students.length; i++) { 
  student = students[i]

  for (var k in student) {
      if (student[k] === input) {
          alert(input)
      } 
  }
}

Hello KRIS NIKOLAISEN ,

Thanks for your response! Coul you walk me through your solution if you have time?

Your code seems to be working. I am not sure about the following statement. I am breaking this a bit:

var students = [

  {name: "Jack", track: "iOS", achievement: 5},
  {name: "Alex", track: "Javascript", achievement: 7},
  {name: "Robert", track: "Python", achievement: 0},
  {name: "Jill", track: "Ruby", achievement: 15},
 {name: "Angus", track: "C++", achievement: 3}
]

for (var i = 0; i < students.length; i++) { 
  student = students[i]

}

If I consol.log the result, I am getting {name: "Angus", track: "C++", achievement: 3}, which is the last object. It seems that your loop is creating a new object "student" that includes the last result of the loop.

The second part of your nested loop starts like this:

for (var k in student) { if (student[k] === input)

My student "student" is Angus. Even when I input Alex I get the last result "Angus". How can student[k] === input?

Thanks for your time!

KRIS NIKOLAISEN Thanks for the explanation!

I am not sure, but it seems I was logging the incorrect data.

If I console.log(student) after the first loop I am able to see all the students, tracks and achievements. I am not sure why it was logging only the last student.

Thanks again!