its*_*ude 2 javascript arrays filter
我对javascript很陌生,并且正在学习一门课程以获取一些经验,但是有时我会在返回概念上破脑筋。基本上,这是我的任务:
有很多不必要的单词。遍历数组以过滤掉这些单词。将剩余的单词保存在名为BetterWords的数组中。有几种方法可以实现这一目标。
我尝试了很多不同的事情...但是我无法使它工作。
代码:
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.filter(function(words){
if(story.includes(unnecessaryWords) === false){
return words;
}
});
console.log(betterWords);Run Code Online (Sandbox Code Playgroud)
我知道你们可能会发现愚蠢的错误,但是我在这里可以从错误中吸取教训,并最终将这个该死的回报概念带入我的脑海(我对简单的回报没有任何疑问,但是当它嵌套时,我感到困惑大声笑)
您应该检查当前单词是否包含在数组中unnecessaryWords:
let betterWords = storyWords.filter(function(currentWord) {
return !unnecessaryWords.includes(currentWord);
});
Run Code Online (Sandbox Code Playgroud)
filter将循环该数组storyWords,并且对于该数组的每个单词currentWord,它将调用一个函数,如果该函数返回true,currentWord则将被包含在结果数组中,否则不会。现在,对于每个currentWord,我们要检查是否unnecessaryWords包含它,如果包含它,我们将返回false,如果不包含,我们将返回true。运营商!是颠倒一个布尔值(如果includes返回true我们要返回false,如果它回到false我们要返回true,所以我们只是用!反转的结果includes和回报)。
该函数期望您在回调中filter返回trueor 。false要检查数组中是否存在某个项目,请使用indexOf. 你不需要使用let除非您打算重新分配变量,否则
结果:
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.';
const overusedWords = ['really', 'very', 'basically'];
const unnecessaryWords = ['extremely', 'literally', 'actually' ];
const storyWords = story.split(' ');
console.log(storyWords.length);
const betterWords = storyWords.filter(function(word){
return unnecessaryWords.indexOf(word) < 0;
});
console.log(betterWords);Run Code Online (Sandbox Code Playgroud)