【发布时间】:2017-12-13 04:43:53
【问题描述】:
我对 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);
我知道你们可能会发现你们眼中愚蠢的错误,但我来这里是为了从我的错误中学习,并最终将这个该死的退货概念带入我的大脑(我对简单的退货没有任何问题,但是当它嵌套时我很困惑哈哈)
【问题讨论】:
-
你不应该调用javascript“java”,因为java是一种完全不同的编程语言......:/
-
你需要在你的代码中清楚地告诉你哪个数组用于
-
如果您只想包含
unnecessaryWords中不存在的字词,请使用function(word) { return !unnecessaryWords.includes(word); }过滤。 -
Iterate over your array哪个数组?
标签: javascript arrays filter