我在js中有以下代码。如果我使用静态数组,一切正常。否则我得到:
Uncaught TypeError: myArray.push is not a function
function test(){
var myArray = localStorage.getItem('articles') ;
//myArray=[1,4,5];
needle = 2 ,
myArray.push(needle);
localStorage.setItem('articles', JSON.stringify(myArray))
console.log(localStorage.getItem('articles'));
}
最佳答案
由于要对其进行字符串化,因此需要将其解析为字符串格式才能使用 push
:
var myArray = JSON.parse(localStorage.getItem("articles"));
关于javascript - 如何将值推送到本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334415/