0

What is the correct syntax when extracting an object property from an array?

example

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

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

var contact = [bob, mary];

console.log(contact[1.phoneNumber]); // <-- Need help here to print phoneNumber!

So when I want to print out the phoneNumber property from mary object using the contact array what is the correct syntax?

1 Answers1

1

console.log(contact[1.phoneNumber]); will lead to an Unexpected token ILLEGAL cause there is not such number.

Try this instead:

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

DEMO

laaposto
  • 11,835
  • 15
  • 54
  • 71