gpt4 book ai didi

JavaScript .push 不工作

转载 作者:行者123 更新时间:2023-11-28 20:27:32 24 4
gpt4 key购买 nike

我完全被这里的这段代码难住了。基本上,我尝试遍历 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com