如何在数组中为对象提供属性?

时间:2017-06-22 04:30:49

标签: javascript arrays

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

在我看来,这就是我所看到的:

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
    }

4 个答案:

答案 0 :(得分:1)

您可以将 if ([yourObject isKindOfClass:[NSNumber class]]) { // It is Number } if ([yourObject isKindOfClass:[NSString class]]) { // it is string } 个对象直接放入数组中:

book

请注意,当您使用构造函数进行扩展(如同拥有数十万个Book对象实例)时,JavaScript引擎可以为具有相同属性的许多对象优化其内部存储器(例如,不存储每个对象实例的属性名称,例如):

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

你会像这样使用它:

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

答案 1 :(得分:0)

这很简单......你已经用book1和book2创建了一个对象,只需将它们变成这样的数组:

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

然后您可以像这样访问图书详细信息(示例将显示第一本书的标题):

 booksListArray[0].title

答案 2 :(得分:0)

编辑:更新了代码,以显示已阅读和未阅读的图书。

使用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);
  }
});




答案 3 :(得分:0)

虽然所有的答案都能满足这个问题。我将提供有关数组的更多信息。

基本上,数组只是事物的集合。这些东西可以是数字,字符串,对象等。在大多数语言中,您可以创建一个只能容纳一种数据类型的数组。例如,在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];

This is how it looks in console

现在回到这个问题,我们需要在一个数组中存储每本书的大量信息。由于数组可以存储基本上是另一种数据结构的对象,该数据结构具有将信息存储在键值对中的能力。

您可以创建描述每本书的对象,然后将每个对象添加到数组

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