我对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);
我知道你的家伙可能会发现你眼中愚蠢的错误但是我在这里向我的错误中学习并最终将这个该死的回归概念带入我的大脑(我的简单回报没有问题但是当它的嵌套我很困惑大笑)
答案 0 :(得分:4)
您应该检查数组unnecessaryWords
中是否包含当前单词:
let betterWords = storyWords.filter(function(currentWord) {
return !unnecessaryWords.includes(currentWord);
});
filter
将循环数组storyWords
,对于该数组的每个单词currentWord
,它将调用一个函数,如果该函数返回true
然后currentWord
1}}将包含在结果数组中,否则不会。现在,对于每个currentWord
,我们要检查unnecessaryWords
是否包含它,如果包含它,我们将返回false
,如果不包含,我们将返回true
。运算符!
用于反转一个布尔值(如果includes
返回true
我们想要返回false
,如果它返回false
我们想要返回{ {1}},因此我们只使用true
来反转!
的结果并返回)。
答案 1 :(得分:0)
filter
函数希望您在回调中返回true
或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);
答案 2 :(得分:0)
您可以执行以下操作,一目了然,您可以轻松阅读和理解。另一个好处是,如果不必要的词语确实不具有动态性,那么不必要的词语的长度很明显,如果您将来审阅/编辑/等等代码,这可能会派上用场
let betterWords = storyWords.filter( (v) =>
(
v !== unnecessaryWords[0] &&
v !== unnecessaryWords[1] &&
v !== unnecessaryWords[2]
)
)
或者,如果AfricWords数组是动态的......
let betterWords = storyWords.filter( (v) =>
return !unnecessaryWords.includes(v)
)
答案 3 :(得分:0)
在本练习中,建议使用.filter()
和.includes()
方法,重要的是说“不包含”(注意!
符号)
let betterWords = storyWords.filter(word => { return !unnecessaryWords.includes(word); })
然后您可以打印结果以使其正常工作:
console.log(betterWords.join(' '));