2017-09-12 2 views
0
let story = 'Last weekend, I took literally the most beautiful bike ride of 
my life. The route is called "The 9W to Nyack" and it actually stretches all 
the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s 
really an adventure from beginning to end! It is a 48 mile loop and it 
basically took me an entire day. I stopped at Riverbank State Park to take 
some extremely artsy photos. It was a short stop, though, because I had a 
really long way left to go. After a quick photo op at the very popular 
Little Red Lighthouse, I began my trek across the George Washington Bridge 
into New Jersey. The GW is actually very long - 4,760 feet! I was already 
very tired by the time I got to the other side. An hour later, I reached 
Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of 
the Hudson. Something that was very surprising to me was that near the end 
of the route you actually cross back into New York! At this point, you are 
very close to the end.'; 

let overusedWords = ['really', 'very', 'basically']; 

let unnecessaryWords = ['extremely', 'literally', 'actually' ]; 

let storyWords = story.split(' '); 
console.log(storyWords.length); 

let betterWords = storyWords.includes(unnecessaryWords); 

上記のJavaScriptコードがあります。ワードの配列メソッドを使用したフィルタ

これはクラス割り当て用です。私たちは配列のイテレータについて学んでいます。 (基本的にここに定義されているメソッド:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

私がすでに行ったことは、ストーリーを含むストリングを設定することです。私は2つの異なる配列を作った.1つは過度に使われた単語と1つは不必要な単語である。

私は、各単語を保存した変数storyから文字列を取り出し、それをstoryWordsという新しい配列に入れた配列を作成しました。

今、不要な単語を削除する配列を作成しようとしています。私はこれらの単語をフィルターに掛けて、残りの単語をbetterWordsという配列に保存するために、配列全体を繰り返したいと思います。

どうすればいいですか?私たちは、ループを使用せず、私たちが学んだイテレータを利用するように言われました。私はこれを達成するために何らかの機能が必要だと確信していますが、私の人生にとってそれを理解することはできません。

現在、私は.filterまたは.includesのいずれかを使用する必要があると考えていますが、どうやってそれをどうやって行くのかは分かりません。

+1

あなたは何を試してみましたか?あなたは単純なループでしようとしていません。 'filter'または' include'が必要だと思うなら - ドキュメントを読んでそれが適合するかどうかを確認してください – Weedoze

+0

あなたの代入とは別に、より簡単な方法は正規表現 'let rg = new RegExp(requiresWords.join( '|')) 、 'gmi'); let betterWords = story.replace(rg、 ''); ' – baao

答えて

1

Array#includeその中と結果を逆にして使用Array#filter

let story = `Last weekend, I took literally the most beautiful bike ride of 
 
my life. The route is called "The 9W to Nyack" and it actually stretches all 
 
the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s 
 
really an adventure from beginning to end! It is a 48 mile loop and it 
 
basically took me an entire day. I stopped at Riverbank State Park to take 
 
some extremely artsy photos. It was a short stop, though, because I had a 
 
really long way left to go. After a quick photo op at the very popular 
 
Little Red Lighthouse, I began my trek across the George Washington Bridge 
 
into New Jersey. The GW is actually very long - 4,760 feet! I was already 
 
very tired by the time I got to the other side. An hour later, I reached 
 
Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of 
 
the Hudson. Something that was very surprising to me was that near the end 
 
of the route you actually cross back into New York! At this point, you are 
 
very close to the end.`; 
 

 
let unnecessaryWords = ['extremely', 'literally', 'actually' ]; 
 

 
let storyWords = story.split(' '); 
 

 
let betterWords = storyWords.filter(sw => !unnecessaryWords.includes(sw.toLowerCase())); 
 

 
console.log(betterWords);

+0

ありがとうございました!私はあなたにも矢印構文を使っていただきありがとうございます。 – hermes

関連する問題