[英]How do I go about giving objects in arrays their properties?
例如,我正在創建一個對象數組,該數組中的每個對象都應該描述一本書。
在我的腦海中,這就是我所看到的:
var booksArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];
我還應該提供以下屬性:
標題:作者:已經閱讀:(布爾值)
所以說這就是我的代碼的樣子,我不知道我是否正確,看看book1,book2等如何未連接到數組。
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
}
您可以將book
對象直接放入數組中:
var books = [
{ title: "Always Running", author: "Luis J. Rodriguez", alreadyRead: true },
{ title: "Hatchet", author: "Gary Paulsen", alreadyRead: true },
// etc
];
請注意,如果要擴展規模(例如,擁有成千上萬的Book對象實例),則使用Constructor函數時,JavaScript引擎可以為許多具有相同屬性的對象優化其內部內存(例如,不存儲屬性名稱)以每個對象實例為例):
function Book(title, author, alreadyRead) {
this.title = title;
this.author = author;
this.alreadyRead = alreadyRead;
}
您將像這樣使用它:
var books = [
new Book( "Always Running", "Luis J. Rodriguez", true ),
new Book( "Hatchet", "Gary Paulsen", true ),
// etc
];
這很容易...您已經創建了一個具有book1和book2的對象,只需將它們分成如下數組即可:
var booksListArray = [
{
title: "Always Running",
author: "Luis J. Rodriguez",
alreadyRead: true
},
{
tite: "Hatchet",
author: "Gary Paulsen",
alreadyRead: true
}
]
然后,您可以訪問像這樣的書詳細信息(示例將顯示第一本書的標題):
booksListArray[0].title
編輯:更新了代碼以顯示已讀和未讀的書。
使用push()
方法將對象插入數組。
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); } });
盡管所有答案都可以滿足這個問題。 我將提供有關數組的更多信息。
基本上,數組只是事物的集合。 這些東西可以是數字,字符串,對象等。在大多數語言中,您可以創建一個只能容納一種數據的數組。 例如,在Java中,數組可以是數字的集合
[1,4,6,2,8]
或字符串
["string1","string2","string with spaces"]
但是不能是不同類型的項目的集合。 您必須先定義數據類型,然后才能使用數組。 例如下面的組合。
[22,"string with numbers", 45.77]
但是在JS中,數組更靈活。 您可以將任何類型的項目存儲在單個數組中。
var bookIds= [1001,1002,1003];
var randomArray = [3,'string',{userID:345},bookIds];
現在回到問題所在,我們需要將每本書的大量信息存儲在一個數組中。 由於數組可以存儲對象,而對象本質上是另一個具有將信息存儲在鍵值對中的數據結構。
您可以創建一個描述每本書的對象,然后將每個對象添加到數組中
var book1 = {
title: "Always Running",
author: "Luis J. Rodriguez",
alreadyRead: true
}
var book2 = {
tite: "Hatchet",
author: "Gary Paulsen",
alreadyRead: true
}
下面是一些聲明數組的方法
//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']
如果數組為空,則需要添加book對象,為此我們可以使用JS提供的push和pop方法
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);
最后一個簡單的練習給您。 創建一個根據以下要求保存信息的數組
我需要存儲一些書籍和作者
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.