Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #29372
    Sidetrack Studio
    Participant

    Playing around with arrays, I have not been successful trying to extract the value for a key. I think it would be console.log( variable name.key); to return all of the keys. So I am expecting this array —

    var people = [
      {
        name: 'Sara',
        age: 25,
        gender: 'f',
        us: true
      },
      {
        name: 'Mike',
        age: 18,
        gender: 'm',
        us: false,
      },
       {
        name: 'Peter',
        age: 17,
        gender: 'm',
        us: false,
      },
       {
        name: 'Ricky',
        age: 27,
        gender: 'm',
        us: false,
      },
      {
        name: 'Martha',
        age: 20,
        gender: 'f',
        us: true
      }
    ];
    
    console.log( people.name );

    to return all of the names for each object in the array. But that is not working. What am I missing?

    #29383
    Christina Hamilton
    Participant

    You have created an array of objects. To access each object, you must index into the array.

    The first object would be people[0].

    The value of “name” in that object would be people[0].name.

    Can you see now how to build a for loop to access all the elements of the array, and pull out the value of name for each one?

    #29410
    Zac Gordon
    Keymaster

    Great response here Christina!

    Yes, in order to get what you’re looking for, you setup would setup a loop like this:

    
    for( let person of people ) {
    
      console.log( person.name );
    
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.