Assessment help - Loops

Hi Guys,

I’m hoping somebody could help me with the second question in this assessment. I’ve been stuck on it for a while. I think I’m thrown by the curly brackets in the array.

If anyone could give me some hits that would be appreciated.

Assessment

Regards,

Jared

@Jared_Cave so on this one, you need to loop through an array that contains JS objects. Remember that arrays can contain anything — basic variables, objects, even other arrays.

In this case, we have the following pattern:

let phonebook = [
      { name : 'Chris', number : '1549' },
      { name : 'Li Kang', number : '9634' },
      { name : 'Anne', number : '9065' },
      { name : 'Francesca', number : '3001' },
      { name : 'Mustafa', number : '6888' },
      { name : 'Tina', number : '4312' },
      { name : 'Bert', number : '7780' },
      { name : 'Jada', number : '2282' },
    ]

The first object is at array index 0, so phonebook[0] will return { name: "Chris", number: "1549" }.

You can return the property values of an object using dot (.) syntax, so phonebook[0].name will return Chris, for example.

Now, putting this together with a loop, you could loop through all the names in the array using phonebook[i].name (substitute i for whatever initializer you use, if it isn’t i. It usually is.)

You can read more about basic object syntax at https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics. This sounds like another example of where I need to provide better references to things that aren’t taught in this module!

I hope this helps. Try this again, and come back to me if you have more questions.