使用数组方法进行字词过滤

时间:2017-09-12 07:06:34

标签: javascript arrays

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

我已经完成的工作是设置一个带故事的字符串。然后我制作了两个不同的数组,一个是过度使用的词,另一个是不必要的词。

然后我创建了一个数组,它将从保存每个单词的变量故事中取出字符串并将其放入一个名为storyWords的新数组中。

现在我正在尝试创建一个删除不必要单词的数组。我想迭代我的数组来过滤掉这些单词并将剩余的单词保存在名为betterWords的数组中。

我该怎么做?我们被告知不要使用for循环并利用我们学到的迭代器。我确信在实现这一目标时我必须做些某种功能,但我不能为我的生活做好准备。

现在我认为我需要使用.filter或.include,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

Array#filter 中使用 Array#include 并反转结果。

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