Javascript 如何为数组中的对象指定其属性?

Javascript 如何为数组中的对象指定其属性?,javascript,arrays,Javascript,Arrays,例如,我正在创建一个对象数组,数组中的每个对象都应该描述一本书 在我的脑海里,我看到的是: var booksArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"]; 我还应该给出以下属性: 标题: 作者: alreadyRead:布尔型 这么说来,这就是我的代码的外观,我不知道我是否做得对,看看book1、boo

例如,我正在创建一个对象数组,数组中的每个对象都应该描述一本书

在我的脑海里,我看到的是:

var booksArray = ["Always Running", "Hatchet", "Autobiography of Malcolm X", "Che Guevara: A Revolutionary Life", "The Prince"];
我还应该给出以下属性:

标题: 作者: alreadyRead:布尔型

这么说来,这就是我的代码的外观,我不知道我是否做得对,看看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
    }
可以将书本对象直接放入数组中:

var books = [
    { title: "Always Running", author: "Luis J. Rodriguez", alreadyRead: true },
    { title: "Hatchet", author: "Gary Paulsen", alreadyRead: true },
    // etc
];
请注意,当您达到中的规模时,如果您使用构造函数,那么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 BookListArray=[永远奔跑,斧头,马尔科姆X的自传,切·格瓦拉:革命生活,王子]; var book1={ 标题:永远奔跑, 作者:路易斯·J·罗德里格斯, 答:是的 } 变量book2={ 标题:斧头, 作者:加里·保尔森, 答:是的 } var账簿=[]; books.pushbook1; books.pushbook2; 控制台、日志; 书{ ifbook.alreadyRead{ console.log'I have read'+book.title+'by'+book.author; }否则{ console.log'I't not 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

var books=[]books.pushbook1;books.pushbook2;试试这个。idk,也许这只是一个复制品。我觉得这是一个经常发布的东西,如果有类似的东西,也许可以将它标记为重复。好吧,我记下来了,我该如何创建一个循环,这样我就可以选择我读过和没有读过的书?我只需要一个例子。不管是真是假,我只需要显示一条消息很简单,你从哪里得到你的作者的名字?你能发布数据源吗?更新了代码以显示已读和未读的书。请查收。