Social Icons

Pages

Tuesday, November 29, 2016

Building an Address Book 2/6

More People
Just like with strings and numbers, we can put multiple objects into an array. We want to practice extracting information from different objects which are stored in the same array.
This allows us to put all of our contact objects into a unified list. If the objects are contact entries, then the list is the book binding that ties all of the contact entries together.



  1. Create an object called mary. It has the same properties as bob. Her name is Mary Johnson, her phoneNumber is "(650) 888 - 8888" and her email is"mary.johnson@example.com".
  2. Create an array called contacts. Put bobin first (at index 0), then mary (at index 1).
  3. Write a console.log statement that prints out Mary's phone number.
?
Hint
Telephone numbers are strings, not numbers!
You can get Mary's object by using standard array notation: arrayName[index].
Remember that the items in an array are numbered starting at 0. Once you have that, don't forget to specify the property you want that is associated to the object.
Arrays can be created just like so:
var myArray = ["item1", "item2", "etc"];




var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnsons",
    phoneNumber: "(650) 888 - 8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

console.log(contacts[1].phoneNumber);

















































No comments:

Post a Comment