【发布时间】:2022-01-23 15:12:13
【问题描述】:
我有一个数组,我想在单击按钮时简单地推动该项目。但是发生的情况是最近推送的数据覆盖了数组的先前数据,我在打印时看到了这一点。我对代码的主要要求是在使用对象/函数的同时实现数组的数据结构,在此先感谢您的帮助。
let BookInfo = [];
//create a storage for the book information
let Book =
{
Name: ' ' ,
Author: ' ' ,
Page: ' ',
addItem : function()
{
Name = document.getElementById('bTitle').value;
Author = document.getElementById('bAuthor').value;
Page = document.getElementById('bPages').value;
this.Name = Name;
this.Author = Author;
this.Page = Page;
BookInfo.push(Book);
document.getElementById('showArr').innerHTML = JSON.stringify(BookInfo);
}
};
【问题讨论】:
-
这里只有 一个
Book对象,所以每次都将同一个对象推送到数组中。 -
@CBroe 先生,你是什么意思?看不懂抱歉。
标签: javascript arrays