JavaScript 数组推送不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27518167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:15:15  来源:igfitidea点击:

JavaScript array push not working

javascriptarraysmethods

提问by urock24

I am trying to push a value into an array and it is giving me this error in the developer tools.

我试图将一个值推送到一个数组中,它在开发人员工具中给了我这个错误。

Uncaught TypeError: Cannot read property 'push' of null

未捕获的类型错误:无法读取 null 的属性“push”

Here is the code that it seems to be sticking on, word and local word were defined earlier like this.

这是它似乎坚持的代码,单词和本地单词之前是这样定义的。

var word = [];
var localWord = []; 

function setLocalArray() {
    // first get words from text field and update word array.
    word = document.getElementById("words").value.split(',');

    // store word array in localStorage            
    for(var i=0; word.length > i; i++) {
        var key2 = "part"+i;
        localStorage.setItem(key2,word[i]);
        localWord.push(key2);
    }

    localStorage.setItem("localWord",JSON.stringify(localWord));
    text2Array();
    reveal();
}

localWord.push(key2); Seems to be what it is getting stuck on. I have looked at everything I can find on the push method and I can't seem to find why it is giving me this error. Help?

localWord.push(key2); 似乎是它被卡住了。我已经查看了可以在 push 方法上找到的所有内容,但似乎无法找到它为什么给我这个错误的原因。帮助?

Here is the full code at jsfiddle http://jsfiddle.net/runningman24/jnLtpb6y/

这是 jsfiddle http://jsfiddle.net/runningman24/jnLtpb6y/的完整代码

采纳答案by urock24

I found the problem, if you look in the jsfiddle I posted I am trying to pull localWord from localStorage even though it doesn't exist and so it sets it to null. Thank you to all for the ideas and contributions.

我发现了问题,如果您查看我发布的 jsfiddle,我试图从 localStorage 中提取 localWord,即使它不存在,因此将其设置为 null。感谢大家的想法和贡献。

回答by Deenadhayalan Manoharan

Try this...

尝试这个...

var localWord = new Array(); //create new array
var word = new Array();

function setLocalArray() {
    word = document.getElementById("words").value.split(',');
    // store word array in localStorage             
    for(var i=0; word.length > i; i++) {
        var key2 = "part"+i;
        localStorage.setItem(key2,word[i]);
        localWord.push(key2);
    }
}

回答by Julio Garcia

You could try isolating the scope of your variable using the module pattern:

您可以尝试使用模块模式隔离变量的范围:

var arrayManager =  (function () {
        var word = [];
        var localWord = []; 

        function setLocalArray() {
            // first get words from text field and update word array.
            word = document.getElementById("words").value.split(',');

            // store word array in localStorage            
            for(var i=0; word.length > i; i++) {
                var key2 = "part"+i;
                localStorage.setItem(key2,word[i]);
                localWord.push(key2);
            }

            localStorage.setItem("localWord",JSON.stringify(localWord));
            text2Array();
            reveal();
        }

       return {
          setLocalArray:setLocalArray
        } ;
    }());

and the from the outside you have to simply call arrayManager.setLocalArray()

从外面你必须简单地调用 arrayManager.setLocalArray()