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

Nuradin Sheikh
Nuradin Sheikh
6,211 Points

I'm not sure where I'm going wrong in my for loop in JavaScript?

I am working on bringing back an array that contains names. I want to bring back the array with all the names except Samir. Can someone point me in the right direction where I'm going wrong? I'm sure it's obvious but I can't seem to see where.

const users = [ {name: 'Samir'}, {name: 'Angela'}, {name: 'Beatrice'} ];

for( i = 0; i < users.length; i++){ if(users.name !== 'Samir'){

console.log(users[i]); } }

1 Answer

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

You're welcome, just remember these concepts:

  • You were trying to access an object literal with users.name, this wasn't working because users is an array of objects.
  • To access items in an array (in this case, objects), you use bracket notation [0] which accesses the first item in the array as they are zero-indexed.
  • Since loops conveniently use an index to iterate through all the items of an array, you can use that users[i].name