Javascript 如何在对象数组上使用localStorage

Javascript 如何在对象数组上使用localStorage,javascript,Javascript,我有一个问题,我真的不知道如何在数组中的对象上创建localStorage。我知道localStorage只用于字符串,但我正在创建一本新书,它是一个对象。怎么做?我是否应该将这本书改为不使用构造函数而只使用setter并在其上使用localStorage,或者如何更改 class Book { constructor(title, author, isbn) { this.title = title; this.author = author; this.

我有一个问题,我真的不知道如何在数组中的对象上创建localStorage。我知道localStorage只用于字符串,但我正在创建一本新书,它是一个对象。怎么做?我是否应该将这本书改为不使用构造函数而只使用setter并在其上使用localStorage,或者如何更改

class Book {
    constructor(title, author, isbn) {
     this.title = title;
     this.author = author;
     this.isbn = isbn;
  }
}

class UI {
static displayBooks() {
    const storedBooks = [
        {
            title: 'Book one',
            author: 'John Doe',
            isbn: '343'
        },
        {
            title: 'Book two',
            author: 'Jane Doe',
            isbn: '455'
        }
    ];

    storedBooks.forEach((book) => UI.addBookToList(book));
}

static addBookToList(book) {
    const list = document.querySelector('#book-list');

    const row = document.createElement('tr');
    row.innerHTML = `
        <td>${book.title}</td>
        <td>${book.author}</td>
        <td>${book.isbn}</td>
        <td><a href="#" class="btn btn-danger btn-sm delete">X</a></td>
    `;

    list.appendChild(row);
}

static deleteBook(el) {
    if (el.classList.contains('delete')) {
        el.parentElement.parentElement.remove();
    }
}

static clearFields() {
    document.querySelector('#title').value = '';
    document.querySelector('#author').value = '';
    document.querySelector('#isbn').value = '';
}

static validateForm() {
    const title = document.querySelector('#title').value;
    const author = document.querySelector('#author').value;
    const isbn = document.querySelector('#isbn').value;

    if (title === '' || author === '' || isbn === '') {
        const div = document.createElement('div');
        div.className = 'alert alert-dismissible alert-danger';
        const message = document.createTextNode('Please fill all fields 
        before adding.');
        div.appendChild(message);

        const bookForm = document.querySelector('#book-form');
        bookForm.parentNode.insertBefore(div, bookForm);


        setTimeout(() => bookForm.parentNode.removeChild(div), 3000);

        return false;
    }

    return true;
   }
 }

document.addEventListener('DOMContentLoaded', UI.displayBooks);

// Add a new book
document.querySelector('#book-form').addEventListener('submit', (e) => {

e.preventDefault();

const title = document.querySelector('#title').value;
const author = document.querySelector('#author').value;
const isbn = document.querySelector('#isbn').value;

const book = new Book(title, author, isbn);

let isSuccess = UI.validateForm();

if (isSuccess) {
    UI.addBookToList(book);        
}

UI.clearFields();
});

document.querySelector('#book-list').addEventListener('click', (e) => {
console.log(e.target);
UI.deleteBook(e.target);
 });
教材{
构造函数(标题、作者、isbn){
this.title=标题;
this.author=作者;
这是isbn=isbn;
}
}
类用户界面{
静态显示书籍(){
常数storedBooks=[
{
标题:"第一册",,
作者:《约翰·多伊》,
isbn:'343'
},
{
标题:"第二册",,
作者:《无名氏》,
isbn:'455'
}
];
storedBooks.forEach((book)=>UI.addBookToList(book));
}
静态addBookToList(书本){
const list=document.querySelector(“#图书列表”);
const row=document.createElement('tr');
row.innerHTML=`
${book.title}
${book.author}
${book.isbn}
`;
list.appendChild(行);
}
静态删除书(el){
if(el.classList.contains('delete')){
el.parentElement.parentElement.remove();
}
}
静态clearFields(){
document.querySelector('#title')。值='';
document.querySelector(“#author”)。值=”;
document.querySelector('#isbn')。值='';
}
静态validateForm(){
const title=document.querySelector(“#title”).value;
const author=document.querySelector(“#author”).value;
const isbn=document.querySelector('#isbn')。值;
如果(标题==''| |作者==''| | isbn==''){
const div=document.createElement('div');
div.className=‘警报可解除警报危险’;
const message=document.createTextNode('请填写所有字段
在加入之前。”);
div.appendChild(消息);
const bookForm=document.querySelector(“#book form”);
bookForm.parentNode.insertBefore(div,bookForm);
setTimeout(()=>bookForm.parentNode.removeChild(div),3000);
返回false;
}
返回true;
}
}
document.addEventListener('DOMContentLoaded',UI.displayBooks);
//添加一本新书
document.querySelector(“#book form”).addEventListener('submit',(e)=>{
e、 预防默认值();
const title=document.querySelector(“#title”).value;
const author=document.querySelector(“#author”).value;
const isbn=document.querySelector('#isbn')。值;
const book=新书(书名、作者、isbn);
让isSuccess=UI.validateForm();
如果(isSuccess){
UI.addBookToList(书籍);
}
UI.clearFields();
});
document.querySelector(“#图书列表”).addEventListener('click',(e)=>{
console.log(如target);
UI.deleteBook(即目标);
});

您可以使用
JSON.stringify()
函数来帮助您实现这一点

下面是一个代码示例:

const bookObj = JSON.stringify(bookObject)
window.localStorage.setItem('book', bookObj)

您可以使用
JSON.stringify()
函数来帮助您实现这一点

下面是一个代码示例:

const bookObj = JSON.stringify(bookObject)
window.localStorage.setItem('book', bookObj)

只需使用
JSON.parse
/
stringify
?数组中对象的可能副本以及如何在其上创建本地存储…我甚至不知道。只需使用
JSON.parse
/
stringify
?数组中对象的可能副本以及如何在其上创建本地存储…即使我也不知道。有一个JSON复活器的现代实现,可以重新创建您的书。有一个JSON复活器的现代实现,可以重新创建您的书。