数组上的筛选方法不起作用

时间:2018-01-20 13:09:15

标签: javascript arrays filter

我正在使用codecademy对迭代器进行练习(迷你短片)。

我有一系列单词,第二个单词包含禁词。 我尝试过滤掉一个不包括禁词的新数组。 但是在控制台中显示数组后,禁用的单词仍然存在。 有人可以帮我解决一下吗?

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'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return word != unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));

非常感谢:)

4 个答案:

答案 0 :(得分:2)

你需要只返回包含检查的否定结果,而不是单词的另一次检查结果。

return !unnecessaryWords.includes(word);



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'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
    return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));




答案 1 :(得分:0)

您可以使用return !unnecessaryWords.includes(word);

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'];
console.log(story.length);
console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));
console.log(betterWords.length);

答案 2 :(得分:0)

您必须使用return !unnecessaryWords.includes(word);

word是一个字符串,unnecessaryWords.includes(word);返回一个布尔值 因此,您要将wordtruefalse

进行比较

例如,您的storyWord数组包含空字符串,我们知道这一点   '' == false将返回true 所以空字符串将从您的数组中删除



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'];
//console.log(story.length);
//console.log(story);
let storyWords = story.split(" ");

let betterWords = storyWords.filter(function(word) {
	return !unnecessaryWords.includes(word);
});

console.log(betterWords.length);
console.log(betterWords.join(' '));




答案 3 :(得分:0)

unnecessaryWords.includes(word)返回布尔值true/false

您不需要比较word != unnecessaryWords.includes(word)

您已使用unnecessaryWords.includes(word)

进行检查

只需返回return !unnecessaryWords.includes(word);

而不是word != unnecessaryWords.includes(word)