Javascript 为什么push函数在对象数组上出错?

Javascript 为什么push函数在对象数组上出错?,javascript,arrays,json,Javascript,Arrays,Json,我试图在javascript中使用.push方法,但它出错了,我不明白为什么 var sc=[]; setItem(“score_history”,JSON.stringify(sc)); setItem(“完美分数”,0); 本地存储的功能评分(完美评分、优胜者评分、优胜者姓名){ var score_history=JSON.parse(localStorage.getItem(“score_history”); 如果(满分

我试图在javascript中使用.push方法,但它出错了,我不明白为什么

var sc=[];
setItem(“score_history”,JSON.stringify(sc));
setItem(“完美分数”,0);
本地存储的功能评分(完美评分、优胜者评分、优胜者姓名){
var score_history=JSON.parse(localStorage.getItem(“score_history”);
如果(满分<10){
var entry=JSON.parse(“{”name:“+”“+winner_name+”,“score:“+winner_score+”);
如果(优胜者得分=24){
var new_score=ParseInt(完美_score);
新的_分数+=1;
setItem(“完美评分”,新评分);
}
得分历史。推(输入);
有什么想法吗

编辑:我发现了错误。它与if语句的处理方式有关

enter code here
document.addEventListener("DOMContentLoaded", function(){
if(localStorage.getItem("perfect_score") == null ||  
localStorage.getItem("score_history") == null){
    var sc = [];
    localStorage.setItem("score_history",JSON.stringify(sc));
    localStorage.setItem("perfect_score",0);
}....
enter code here
var s=[];
   localStorage.setItem("score_history",JSON.stringify(s));

    var score_history = JSON.parse(localStorage.getItem("score_history"));
    var perfect_score = localStorage.getItem("perfect_score");

    if(perfect_score < 10){


        var entry = JSON.parse('{ "name": '+'"'+winner_name+'", "score": '+winner_score+"}");


        if(winner_score == 24){
            var new_score = parseInt(perfect_score);
            new_score += 1;
            localStorage.setItem("perfect_score", new_score);
        }

        score_history.push(entry);
在此处输入代码
document.addEventListener(“DOMContentLoaded”,function()){
if(localStorage.getItem(“完美分数”)==null
localStorage.getItem(“score\u history”)==null){
var sc=[];
setItem(“score_history”,JSON.stringify(sc));
setItem(“完美分数”,0);
}....
在这里输入代码
var s=[];
setItem(“score_history”,JSON.stringify);
var score_history=JSON.parse(localStorage.getItem(“score_history”);
var perfect_score=localStorage.getItem(“perfect_score”);
如果(满分<10){
var entry=JSON.parse(“{”name:“+”“+winner_name+”,“score:“+winner_score+”);
如果(优胜者得分=24){
var new_score=parseInt(完美_score);
新的_分数+=1;
setItem(“完美评分”,新评分);
}
得分历史。推(输入);
以前,查看日志时,score_history将返回一个字符串,但现在它只返回正确的“[]”。
现在我的问题是,为什么我的程序不喜欢if语句中处理它的方式?

根据我从您的错误中了解到的,变量
score\u history
不是数组。它可能是
null
未定义的
。您可以使用
|
)验证变量是否为空的运算符:

var score_history=JSON.parse(localStorage.getItem(“score_history”))|【】


此代码将检查
JSON.parse(localStorage.getItem(“score\u history”)的值是否为
为空,如果为空,则将变量
score\u history
初始化为空数组。

您能发布它给您带来的错误吗?尤其是错误消息。@Nicolas已编辑。您正在某处将
score\u history
设置为数组以外的内容。目前您仅在此处共享初始设置项
localStorage.setItem(“score_history”,JSON.stringify(sc))
。在
score_history.push(entry)
之后,在哪里设置该键?这不是
JSON。stringify(“score_history”)
只是stringify一个字符串。您需要获取数组的JSON字符串。它应该是
localStorage.setItem(“score_history”,JSON.stringify(scorehistory))
No,您已经编写了
JSON.stringify(“score\u history”)
。它应该是
JSON.stringify(scorehistory)
您正在字符串化一个名为
“score\u history”
的字符串。您应该改为字符串化数组
scorehistory