javascript - 在本地存储中设置对象的值
问题描述
需要在本地存储中添加对象并同时使用它们。但显示“noteObj.push 不是函数”的代码
// add a Event Listener in #add-btn and gat 2 input textlet addBtn = document.getElementById("add-btn");
addBtn.addEventListener("click", function (e) {
let addTextHeading = document.getElementById('add-text-heading').value;
let addTextBody = document.getElementById('add-text-body').value;
// get value form localStorage
let notes = localStorage.getItem('notes');
if (notes == null) {
noteObj = []
}
else {
noteObj = JSON.parse(notes)
}
// add value in localStorage
let myObj = {
heading: addTextHeading,
body: addTextBody
}
noteObj.push(myObj)
localStorage.setItem("notes", JSON.stringify(noteObj))
})
解决方案
推荐阅读
- c# - 将异常类型更改为基类
- python - Pandas 了解 csv 文件的不一致分隔符
- google-analytics - 根据开发环境分离 Google Analytics 数据
- python - 使用python selenium单击具有相同链接文本的多个链接
- python - 框架没有响应 face_recognition 库
- maven - 当它有父pom时如何正确地将工件部署到nexus?
- typescript - Typescript合并了具有不同有效负载的数组中两个对象的类型
- javascript - vuejs - 无法让 dayjs 在浏览器中工作
- python-3.x - kinesis firehose 如何更改文件名和文件夹层次结构
- typescript - react-intl 中 intl 的 typescript 类型是什么?