2016-08-06 32 views
0

Ich versuche, eine Methode zu schreiben, die die Wörter in einem Satz nimmt, listet die verschiedenen Wörter und die Anzahl der Male auftreten, aber ich kann nicht erhalten es funktioniert.Zuweisen von Array-Elementen als Objekteigenschaften mit 'Zähler' Wert

Dies ist, was ich bisher:

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; 

Am Ende davon Ich hoffe, dies zu sehen zurück:

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

Meine Konsole sagt mir,

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

Ich nehme an undefined bezieht sich auf die counter Variable. Ich habe versucht, dies innerhalb und vor der Schleife zu deklarieren (wie 0), aber das funktioniert nicht.

Wird etwas mit wordsArr[i] als String zurückgegeben und daher nicht richtig als Eigenschaft von wordsObj festgelegt?

Wenn mir jemand sagen kann, wo ich falsch liege, würde ich es begrüßen. Danke im Voraus.

Antwort

1

die Eigenschaft Spur der gezählten Fällen zu halten, so dass Sie eine counter Variable nicht brauchen:

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

auseinander bilden, dass Sie einige kleinere Syntaxfehler hatte ...

+0

Genau was ich suchte. Es ist nicht vorgekommen, Bracket-Notation zu verwenden, um die Eigenschaft hinzuzufügen, die ein indiziertes Array-Element ist, oder einfach die Eigenschaft zu verwenden, um den vorhandenen Zählerwert zurückzugeben ... Danke! – Paulos3000

+0

froh, dass dir geholfen hat! – maioman

0

Eine andere Möglichkeit, dies zu tun, könnte

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