我在这里完全被这些代码所困扰。基本上,我正在尝试浏览一个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的下一个日志给了我[“我”]。
这里有什么问题?
编辑:完整相关代码
//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;
}
}
答案 0 :(得分:1)
这是一个JSFiddle示例。
您的代码对示例所做的唯一更改:
wordList的定义
在jsfiddle示例中,将输出附加到
它似乎做你想要的。你对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);
});
答案 1 :(得分:1)
问题原因: 数据文件已被修改,VERBS不再被正确标记,导致没有生成句子
为什么数组是空的
系统用于处理数组清空它们,新变量asdf
仅指向到数组,当我在控制台中使用它时它是空的
我怎么能避免这么多:
case "1": nouns.push(wordList[i].word); asdf = nouns.slice(0); break;
让这成为公共服务公告。在调试期间复制数组。
答案 2 :(得分:1)
今天我遇到了同样的问题。花了一些时间才弄清楚我在Chrome调试器中有一个清空数组的Watch Expression ..
所以推送的代码看起来像这样:
$scope.emailMsg.Bcc.push(EmailAddress);
在调试器中观察表达式,如下所示
$scope.emailMsg.Bcc = [];
所以 - 每次我踩到我的阵列都会归零:)