我的localStorage结果显示[null,null,"某些"]]而不是"某些东西"

时间:2016-05-01 16:52:56

标签: javascript json local-storage

我的问题是结果显示为[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]...

1 个答案:

答案 0 :(得分:1)

获取localStorage值,如下所示(正确方式)。

错误的方式

var showsubjek = localStorage["subject"];

正确的方式

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