gpt4 book ai didi

javascript - 在 Javascript 中创建具有自己属性的对象数组

转载 作者:行者123 更新时间:2023-12-02 16:30:05 27 4
gpt4 key购买 nike

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"];

我试图弄清楚如何创建一个具有自己属性的对象数组,但这似乎不起作用。我不明白发生了什么事。

编辑:

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

这是更新后的代码。这仍然不起作用。

编辑2:在document.write中缺少“+”。

最佳答案

您正在将数组传递给数组,而不是对象。

  1. [ 更改为 {,因为第二个是正确的对象定义。
  2. 不要使用 var 两次。一旦有了变量,就可以在没有 var 的情况下使用它。

    var Book = new Array();

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

你可以像这样使用它:

alert(Book[0].title);

关于javascript - 在 Javascript 中创建具有自己属性的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com