[英]Assigning array elements as object properties, with 'counter' value
I'm trying to write a method that takes the words in a sentence, lists the different words, and the number of times they occur, but I can't get it to work. 我正在尝试编写一种方法,将句子中的单词,列出不同的单词以及出现的次数列出,但是我无法使其正常工作。
This is what I have so far: 这是我到目前为止的内容:
var wordsArr = ['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish']
var wordsObj = {};
for (var i = 0; i < wordsArr.length; i++) {
var counter = 0;
wordsObj.wordArr[i] = counter++;
}
return wordsObj;
At the end of it I'm hoping to see this returned: 最后,我希望看到返回的结果:
{ one : 1, fish : 4, two : 1, red : 1, blue : 1 }
My console is telling me, 我的控制台告诉我,
'Uncaught TypeError: Cannot set property '0' of undefined'
'未捕获的TypeError:无法设置未定义的属性'0'
I assume undefined
is referring to the counter
variable. 我假设
undefined
是指counter
变量。 I've tried declaring this both within and before the loop (as 0
), but this doesn't work. 我试过在循环内和循环前声明为
0
,但这是行不通的。
Is it something to do with wordsArr[i]
being returned as a string and therefore not being set properly as a property of wordsObj
? 这与
wordsArr[i]
作为字符串返回并且因此未正确设置为wordsObj
的属性有关吗?
If anyone can tell me where I'm going wrong I'd appreciate it. 如果有人可以告诉我我要去哪里错了,我将不胜感激。 Thanks in advance.
提前致谢。
the property will keep track of the counted instances so you don't need a counter
variable: 该属性将跟踪计数的实例,因此您不需要
counter
变量:
var wordsArr = ['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish'] var wordsObj = {}; for (var i = 0; i < wordsArr.length; i++) { //var counter = 0; wordsObj[wordsArr[i]] = ( wordsObj[wordsArr[i]] || 0 ) + 1; } console.log( wordsObj );
apart form that you had some minor syntax errors ... 分开的形式,你有一些小的语法错误...
One other way of doing this could be 另一种方法是
var longText = "These excellant intentions were strengthed when he enterd the Father Superior's diniing-room, though, stricttly speakin, it was not a dining-room, for the Father Superior had only two rooms alltogether; they were, however, much larger and more comfortable than Father Zossima's. But tehre was was no great luxury about the furnishng of these rooms eithar. The furniture was of mohogany, covered with leather, in the old-fashionned style of 1820 the floor was not even stained, but evreything was shining with cleanlyness, and there were many chioce flowers in the windows; the most sumptuous thing in the room at the moment was, of course, the beatifuly decorated table. The cloth was clean, the service shone; there were three kinds of well-baked bread, two bottles of wine, two of excellent mead, and a large glass jug of kvas -- both the latter made in the monastery, and famous in the neigborhood. There was no vodka. Rakitin related afterwards that there were five dishes: fish-suop made of sterlets, served with little fish paties; then boiled fish served in a spesial way; then salmon cutlets, ice pudding and compote, and finally, blanc-mange. Rakitin found out about all these good things, for he could not resist peeping into the kitchen, where he already had a footing. He had a footting everywhere, and got informaiton about everything. He was of an uneasy and envious temper. He was well aware of his own considerable abilities, and nervously exaggerated them in his self-conceit. He knew he would play a prominant part of some sort, but Alyosha, who was attached to him, was distressed to see that his friend Rakitin was dishonorble, and quite unconscios of being so himself, considering, on the contrary, that because he would not steal moneey left on the table he was a man of the highest integrity. Neither Alyosha nor anyone else could have infleunced him in that.", words = longText.split(" ").map(w => w.toLowerCase()), wordCount = words.reduce((p,c) => (p[c] === void 0 ? p[c] = 1 : ++p[c],p),{}); console.log(wordCount);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.