【问题标题】:Assigning array elements as object properties, with 'counter' value将数组元素分配为对象属性,具有“计数器”值
【发布时间】:2016-08-06 10:25:49
【问题描述】:

我正在尝试编写一个方法来获取句子中的单词,列出不同的单词以及它们出现的次数,但我无法让它工作。

这是我目前所拥有的:

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;

最后我希望看到它返回:

{ one : 1, fish : 4, two : 1, red : 1, blue : 1 }

我的控制台告诉我,

'Uncaught TypeError: Cannot set property '0' of undefined'

我假设undefined 指的是counter 变量。我尝试在循环内和循环之前声明这一点(如0),但这不起作用。

这是否与 wordsArr[i] 作为字符串返回并因此未正确设置为 wordsObj 的属性有关?

如果有人能告诉我哪里出错了,我将不胜感激。提前致谢。

【问题讨论】:

    标签: javascript arrays object for-loop counter


    【解决方案1】:

    该属性将跟踪计数的实例,因此您不需要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 );

    你有一些小的语法错误...

    【讨论】:

    • 正是我所追求的。没有使用括号表示法来添加属性,作为索引数组元素,或者只是使用属性来返回现有的计数器值......谢谢!
    • 很高兴为您提供帮助!
    【解决方案2】:

    另一种方法是

    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);

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 1970-01-01
      • 2016-07-25
      • 2021-12-07
      • 2022-07-03
      • 1970-01-01
      • 2019-05-14
      • 2018-09-23
      • 1970-01-01
      相关资源
      最近更新 更多