Henry Merida
Henry Merida

Reputation: 25

How do I go about giving objects in arrays their properties?

For example, I'm creating an array of objects, and each object in that array is supposed to describe a book.

In my head this is what I see:

var booksArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];

I'm also supposed to give the following properties:

Title: Author: alreadyRead: (boolean)

SO..with that said this is how my code looks, I don't know if I'm going about it right, seeing how book1, book2, etc are not connected to the array.

    var booksListArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];

    var book1 = {
        title: "Always Running",
        author: "Luis J. Rodriguez",
        alreadyRead: true
    }

    var boook2 = {
        tite: "Hatchet",
        author: "Gary Paulsen",
        alreadyRead: true
    }

Upvotes: 0

Views: 138

Answers (4)

Hassan Imam
Hassan Imam

Reputation: 22574

EDIT : Updated the code to display the read as well as not read book.

Use push() method to insert object into array.

var booksListArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];

var book1 = {
    title: "Always Running",
    author: "Luis J. Rodriguez",
    alreadyRead: true
}

var book2 = {
    title: "Hatchet",
    author: "Gary Paulsen",
    alreadyRead: true
}
var books = [];
books.push(book1);
books.push(book2);
console.log(books);
    
books.forEach(function(book){
  if(book.alreadyRead){
    console.log('I have read ' + book.title + ' by ' + book.author);
  } else {
    console.log('I haven\'t read ' + book.title+ ' by ' + book.author);
  }
});

Upvotes: 0

amt8u
amt8u

Reputation: 29

Although all the answers would satisfy the question. I would be giving a some more info about arrays.

Basically arrays are just collections of things. Those things can be numbers, strings, objects etc. In most languages you can create an array which can hold only one type of data. For eg in Java, an array can be a collection of numbers

[1,4,6,2,8] 

or maybe strings

["string1","string2","string with spaces"]

But cannot be a collection of items of different types. You have to define the type of data before you can use the array. For eg the below combination.

[22,"string with numbers", 45.77]

But in JS, arrays are more flexible. You can store any type of item in a single array.

var bookIds= [1001,1002,1003];    
var randomArray = [3,'string',{userID:345},bookIds];

This is how it looks in console

Now coming back to the question, we need to store a lot of information for each book in a single array. Since array can store objects which essentially is another data structure having the capacity to store information in key value pairs.

You can create an object that describes each book and then add each object to the array

var book1 = {
    title: "Always Running",
    author: "Luis J. Rodriguez",
    alreadyRead: true
}
var book2 = {
    tite: "Hatchet",
    author: "Gary Paulsen",
    alreadyRead: true
}

Some of the ways you can declare arrays are below

//declaring empty array
var books = []; 

// by doing this, you are just creating an array of strings where each string gives you the name of the book
var booksArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];

//declaring and initializing at the same time with two objects- book1 and book2, of course both need to be declared before this line
var books = [book1, book2] 

//declaring array with some book IDs. These books ids then can be used to access other arrays or objects which contain all the books information.
var books = ['1001', '1002'] 

In case of empty arrays, we would need to add book objects, for which we can use the push and pop methods provided by JS

var book1 = {
    title: "Always Running",
    author: "Luis J. Rodriguez",
    alreadyRead: true
}
var book2 = {
    tite: "Hatchet",
    author: "Gary Paulsen",
    alreadyRead: true
}
books.push(book1);
books.push(book2);

A simple exercise in the end for you. Create an array that holds information according to the below requirement

I need to store a collection of books and authors

Book details: 
book_id - should be a number
name - should be a string
authors - a list of authors as there can be multiple authors for one book, here you can again use array of author_ids
alreadyRead - a boolean value

Author details : 
author_id - author id, should be a number
name - should be a string
books - a list of books written, maybe an array of book_ids that should represent the books this author has written

Upvotes: 0

Sholeman
Sholeman

Reputation: 1

It's easy... you have already created an object with book1 and book2 already, just make them into array like this:

var booksListArray = [
   {
    title: "Always Running",
    author: "Luis J. Rodriguez",
    alreadyRead: true
   },
   {
    tite: "Hatchet",
    author: "Gary Paulsen",
    alreadyRead: true
   }
  ]

Then you can access the book details like this (example will display title of first book):

 booksListArray[0].title

Upvotes: 0

Dai
Dai

Reputation: 155503

You can put the book objects directly into an array:

var books = [
    { title: "Always Running", author: "Luis J. Rodriguez", alreadyRead: true },
    { title: "Hatchet", author: "Gary Paulsen", alreadyRead: true },
    // etc
];

Note that when you get to scale (as in, having hundreds of thousands of Book object instances) if you use a Constructor function then the JavaScript engine can optimize its internal memory for many objects with the same properties (e.g. by not storing the property names with every object instance, for example):

function Book(title, author, alreadyRead) {
    this.title = title;
    this.author = author;
    this.alreadyRead = alreadyRead;
}

And you would use it like so:

var books = [
    new Book( "Always Running", "Luis J. Rodriguez", true ),
    new Book( "Hatchet", "Gary Paulsen", true ),
    // etc
];

Upvotes: 1

Related Questions