0
var i;


    var Book = new Array();
    var Book[0] = [title: "1984", author: "George Orwell", publisher: "Harvill Secker", price: "€8.99"];
    var Book[1] = [title: "Metro 2033", author: "Dmitry Glukhovsky", publisher: "Orionbooks", price: "€12.99"];
    var Book[2] = [title: "Always Outnumbered, Always Outgunned", author: "Walter Mosley", publisher: "W. W. Norton & Company", price: "€5.99"];    
    var Book[3] = [title: "Journey to the Center of the Earth", author: "Jules Verne", publisher: "Pierre Jules Hetzel", price: "€4.99"];

I'm trying to figure out how to make an array of objects that have their own properties but this doesn't seem to work. I can't understand what's going on.

EDIT:

    var i;
    var Book = new Array();
    Book[0] = {title: "1984", author: "George Orwell", publisher: "Harvill Secker", price: "€8.99"};
    Book[1] = {title: "Metro 2033", author: "Dmitry Glukhovsky", publisher: "Orionbooks", price: "€12.99"};
    Book[2] = {title: "Always Outnumbered, Always Outgunned", author: "Walter Mosley", publisher: "W. W. Norton & Company", price: "€5.99"};    
    Book[3] = {title: "Journey to the Center of the Earth", author: "Jules Verne", publisher: "Pierre Jules Hetzel", price: "€4.99"};

    for (i = 0; i < 4; i++)
    {
        document.write("Book: " + Book[i].title + "Author: " + Book[i].author "Publisher: " + Book[i].publisher + "Price: " + Book[i].price);
    }

This is the updated code. This still doesn't work.

EDIT 2: Was missing a "+" in the document.write.

2
  • 3
    Object literal notation uses curly brackets: {title: '1984', author: 'George...}. Square brackets are for array literals. Commented Feb 7, 2015 at 22:06
  • use Book[i].author + "Publisher: " instead of Book[i].author "Publisher: " Commented Feb 7, 2015 at 22:19

2 Answers 2

2

You are passing array to your arrays, not objects.

  1. Change [ for {, since second is proper object definition.
  2. Don't use var twice. Once you have variable, you can use it without var.

    var Book = new Array();
    
    Book[0] = {title: "1984", author: "George Orwell", publisher: "Harvill Secker", price: "€8.99"};
    

You can use it like this:

alert(Book[0].title);
Sign up to request clarification or add additional context in comments.

3 Comments

Okay I have done this and it still doesn't work and updating the original post. The objects do not print via document.write. Can I not use objects in that?
it works, but you have lack of + in your document.write parameter. write: Book[i].author +"Publisher: "
Well. That's a huge mistake. Will update the top post.
0

The syntax is incorrect.

Array is: [ ] Object is: { }

Besides the Book variable must be declared once.

The correct way is :

 var Book = new Array();
 Book[0] = {title: "1984", author: "George Orwell", publisher: "Harvill Secker", price: "€8.99"};
 Book[1] = {itle: "Metro 2033", author: "Dmitry Glukhovsky", publisher: "Orionbooks", price: "€12.99"};
 Book[2] = {title: "Always Outnumbered, Always Outgunned", author: "Walter Mosley", publisher: "W. W. Norton & Company", price: "€5.99"};    
 Book[3] = {title: "Journey to the Center of the Earth", author: "Jules Verne", publisher: "Pierre Jules Hetzel", price: "€4.99"};

Or

Book.push ( {title: "1984", author: "George Orwell", publisher: "Harvill Secker", price: "€8.99"}) 
//...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.