我完全被这里的这段代码所困扰。基本上,我试图遍历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 - JavaScript .push无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17099736/