我的问题是结果显示是[null,null,"something"]]而不是"something"

我的代码如下所示:

var simpan = JSON.parse(localStorage.getItem('subject')) || [];
                        simpandata.push(subjek);
                        localStorage.setItem('subject', JSON.stringify(simpandata));

var showsubjek = localStorage["subject"];
        showsubjek = JSON.stringify(showsubjek);
        showsubjek2 = JSON.parse(showsubjek);
        document.getElementById("getItemitu").innerHTML=showsubjek2;


然后,如果我删除此代码:showsubjek = JSON.stringify(showsubjek);
结果将显示为[object, object]...

最佳答案

如下所示获取localStorage值(正确的方法)。

错误的方法

var showsubjek = localStorage["subject"];


正确的方法

var showsubjek = localStorage.getItem("subject");

09-09 23:04