JavaScript .push 不工作

标签 javascript arrays push

我完全被这里的这段代码难住了。基本上,我尝试遍历 Word 对象数组,并使用 switch 语句根据单词类型组织它们。这都是由 jQuery 等待按钮被按下而触发的。

for (var i=0; i<wordList.length; i++)
{
    switch (wordList[i].type) {
        case "1": nouns.push(wordList[i].word); break;
        //"1" is the type flag for noun, the "word" property is the string containing the word
        ... //Rest of word types
    }
}

该单词实际上不会分配给名词数组。所以我将案例“1”行更改为:

case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;

如果没有 var,asdf 和 asdf2 就变成隐式全局的,所以我可以在控制台中使用它们:

asdf
asdf2

分别返回了 [] 和“I”,因此它可以拾取该单词,但没有将其添加到数组中。

asdf.push(asdf2)

返回1,asdf的下一个日志给了我[“I”]。

这里出了什么问题?

编辑:完整相关代码

//Declare arrays
var articles=[], properNouns=[], nouns=[], pluralNouns=[], adjectives=[], conjunctions=[], verbs=[], pluralVerbs=[], adverbs=[], prepositions=[], interrogatives=[];

//Sort words into proper arrays
for (var i=0; i<wordList.length; i++)
{
    switch (wordList[i].type) {
        case "1": nouns.push(wordList[i].word); asdf = nouns; asdf2 = wordList[i].word; break;
        case "11": pluralNouns.push(wordList[i].word); break;
        case "12": properNouns.push(wordList[i].word); break;
        case "2": verbs.push(wordList[i].word); break;
        case "21": pluralVerbs.push(wordList[i].word); break;
        case "3": adjectives.push(wordList[i].word); break;
        case "4": adverbs.push(wordList[i].word); break;
        case "5": conjunctions.push(wordList[i].word); break;
        case "6": prepositions.push(wordList[i].word); break;
        case "7": interrogatives.push(wordList[i].word); break;
        case "8": articles.push(wordList[i].word); break;
        default: console.log("Error, could not sort "+wordList[i].word); break;
    }
}

最佳答案

这是一个JSFiddle示例。

您的代码对示例所做的唯一更改:

  • wordList的定义

  • 在 jsfiddle 示例中,一个 div 标记将输出附加到

它似乎做你想做的事。你对wordList的定义不同吗?

    $(document).ready(function () {
    //Declare arrays
    var articles = [], properNouns = [], nouns = [], pluralNouns = [], adjectives = [], conjunctions = [], verbs = [], pluralVerbs = [], adverbs = [], prepositions = [], interrogatives = [];

    var wordList = [{ 'type': "1", 'word': 'foo' },
         { 'type': "1", 'word': 'foo1' },
         { 'type': "1", 'word': 'foo2'}, 
         { 'type': "1", 'word': 'foo3' }];

    //Sort words into proper arrays
    for (var i = 0; i < wordList.length; i++) {
        switch (wordList[i].type) {
            case "1":
                nouns.push(wordList[i].word);
                asdf = nouns;
                asdf2 = wordList[i].word;
                break;
            case "11":
                pluralNouns.push(wordList[i].word);
                break;
            case "12":
                properNouns.push(wordList[i].word);
                break;
            case "2":
                verbs.push(wordList[i].word);
                break;
            case "21":
                pluralVerbs.push(wordList[i].word);
                break;
            case "3":
                adjectives.push(wordList[i].word);
                break;
            case "4":
                adverbs.push(wordList[i].word);
                break;
            case "5":
                conjunctions.push(wordList[i].word);
                break;
            case "6":
                prepositions.push(wordList[i].word);
                break;
            case "7":
                interrogatives.push(wordList[i].word);
                break;
            case "8":
                articles.push(wordList[i].word);
                break;
            default:
                console.log("Error, could not sort " + wordList[i].word);
                break;
        }
    }
    for (var i in nouns) {
        console.log(nouns[i]);
        $('#output').append(nouns[i] + '<br>');
    }
    console.log(nouns);
});

关于JavaScript .push 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17099736/

相关文章:

javascript - 使用 jquery 抓取 HTML 并操作元素

arrays - 转置数组和自动填充

java - java比较同一个数组的元素,然后删除该元素

github - 所有者无法推送到组织存储库

javascript - getElementsByClassname 不起作用

javascript - MDN 对 __proto__ 属性的解释可能有错误?

php - 如何从PHP数组生成一定数量的值?

php - MongoDB和PHP可以添加同名字段吗?

git - 启用 2FA 时推送到 GitHub 存储库而无需个人访问 token

javascript - 将 Knockout.js 与自定义模板引擎结合使用