简体   繁体   English

将数组元素分配为具有“计数器”值的对象属性

[英]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.

相关问题 根据其他对象数组的值动态分配 object 属性 - Dynamically assigning object properties based on the value of an array of other objects 如何返回一个数组,其中包含 object 中的前 2 个元素,其值属性是一个数组 - How to return an array with the first 2 elements in an object whose value properties are an array 将对象属性值分配给一个或多个变量 - Assigning object properties values to an array or variables 用对象属性替换数组元素 - Replace array elements with object properties 在jQuery对象数组中为对象分配新值 - assigning new value to an object in object array in jquery 是否有任何值可以重新定义对象字段或数组元素,而不是将引用分配给其他对象 - Is there any value to redefining objects fields or array elements instead of assigning reference to other object 分配Javascript对象属性 - Assigning Javascript Object Properties 将字符串数组的动态值分配给 typescript 中 object 的属性 - Assigning dynamically values of string array to properties of an object in typescript Javascript:无法读取为对象赋值的未定义属性(读取“0”) - Javascript: Cannot read properties of undefined (reading '0') which assigning value to object 如何在JavaScript中将数组中的元素添加为对象的属性 - How to add elements in an array as properties of an object in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM