javascript - 使用数组方法的单词过滤器

标签 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)

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

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

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

我该怎么做?我们被告知不要使用 for 循环并利用我们学到的迭代器。我确定我必须执行某种功能才能完成此操作,但我终究无法弄明白。

现在我在想我需要使用 .filter 或 .includes,但我不确定该怎么做。

最佳答案

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

关于javascript - 使用数组方法的单词过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170046/

相关文章:

arrays - 翻转元素以最小化最大的连续元素在一起

Javascript 练习 - 反转二维数组

java - 如何区分父类(super class)数组中的不同子类?

javascript - 为什么 String.prototype.split 返回完整的字符串

javascript - 从 firefox 插件发送帖子请求

javascript - AngularJS 指令只调用一次?

python - 将 ImageDataGenerator 结果分配给 Numpy 数组

java - 将文本文件读入构造函数数组(java)

javascript - 在 Django 中显示来自 API 的 JSON 数据

javascript - URL href 失败 : Anchors, URL 和 Javascript