gpt4 book ai didi

javascript - 如何在 Javascript 中向数组添加其他对象

转载 作者:行者123 更新时间:2023-11-28 12:25:45 25 4
gpt4 key购买 nike

我希望了解如何在我创建的构造函数之后将一个对象添加到具有多个现有对象的数组中。

//constructor function
function Bookdes(title, author, pages, current_page, available){
this.title = title;
this.author = author;
this.pages = pages;
this.current_page = current_page;
this.available = available;
}
//array of books
var bookarrays = [
new Bookdes("Fast Cars", "John Burns", 205, 43, "true"),
new Bookdes("Slow Cars", "Joe Fast", 70, 32, "false" ),
new Bookdes("Big Cars", "Darla Jean", 234, 42, "true"),
new Bookdes("Small Cars", "Dema Jones", 214, 34, "false"),
new Bookdes("Cars", "Alex Best", 235, 41, "true")
];

//Add two more books
bookarrays.push("Gasoline Car", "Johnny Walker", 200, 31, "true");
bookarrays.push("Natural gas car", "James Dean", 150, 21, "true");
console.log(bookarrays);

谢谢

最佳答案

问题是你没有调用构造函数。正确的调用是:

bookarrays.push(new Bookdes("Gasoline Car", "Johnny Walker", 200, 31, "true"));

关于javascript - 如何在 Javascript 中向数组添加其他对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29067781/

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