2016-08-06 98 views
0

我試圖寫一個方法,需要在一個句子中的單詞,列出不同的單詞和它們發生的次數,但我無法獲得它工作。分配數組元素作爲對象屬性,'計數器'值

這是我到目前爲止有:

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的屬性?

如果有人能告訴我我要去哪裏,我會很感激。提前致謝。

回答

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

除了形成你有一些小的語法錯誤...

+0

正是我以後。沒有發生使用括號表示法來添加屬性,作爲索引數組元素,或只是使用該屬性來返回現有的計數器值...謝謝! – Paulos3000

+0

很高興幫助你! – maioman

0

這樣做的另一種方式可能是

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

相關問題