这是我正在参加Codecademy的一门课程,但我遇到了困难。我真的很想对JS很好地了解
我正在进行的任务是: “有一个不需要的单词数组。遍历您的数组以过滤掉这些单词。将其余的单词保存在名为BetterWords的数组中。有几种方法可以实现。”
到目前为止,这就是我所拥有的,但是我一直试图找出如何将“必要”的单词返回到“ 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);
storyWords.filter(function() {
let betterWords = [];
for (wordsIndex= 0; wordsIndex < storyWords.length; wordsIndex++) {
for (unnecessaryWordsIndex = 0; unnecessaryWordsIndex < unnecessaryWords.length; unnecessaryWordsIndex++) {
if (wordsIndex != unnecessaryWordsIndex) {
}
}
}
});
我会使用.splice()
删除项目,然后使用.map()
吗?
我知道我可能正在使这种方式比应有的方式复杂,因为这就是我要做的!任何建议和解释都将是美好的,并提前感谢您!
答案 0 :(得分:1)
您可以使用Array#filter
的结果过滤单词,然后使用Array#includes
检查unnecessaryWords
中单词是否不是,然后 不是在overusedWords
数组中。
var 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.',
overusedWords = ['really', 'very', 'basically'],
unnecessaryWords = ['extremely', 'literally', 'actually' ],
storyWords = story.split(' '),
betterWords = storyWords.filter(word =>
!unnecessaryWords.includes(word) &&
!overusedWords.includes(word)
);
document.body.appendChild(document.createTextNode(betterWords.join(' ')));
答案 1 :(得分:1)
您正在混淆filter
函数的用途。您应该这样做:
let betterWords = storyWords.filter(function(word) {
if (overusedWords.includes(word)) return false;
if (unnecessaryWords.includes(word)) return false;
return true;
});
filter
不会更改原始数组。
答案 2 :(得分:0)
x.labels
返回一个新数组,因此您应该做类似filter
答案 3 :(得分:0)
我不建议将overusedWords
和unnecessaryWords
保留为数组。在这些数组中进行查找是 O(n) -您每次执行查找时都必须检查数组中的每个元素。如果将这些数组转换为集合,则会得到 O(1)查找。对于这么小的输入,性能差异可以忽略不计,但是代码易于编写且干净整洁,如果您有大量的过度使用/不必要的单词,这将可以扩展。
此外,我建议拆分非单词字符而不是空格。如果您输入的内容中包含“ literally”一词(后跟一个逗号),那么您当前的方法将错过该词。
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'];
const toRemove = new Set(overusedWords.concat(unnecessaryWords));
const filtered = story.split(/\W+/).filter(e => !toRemove.has(e.toLowerCase()));
console.log(filtered);
这也可以通过忽略大小写的正则表达式单行代码完成,并为您提供一些潜在的替换/清除选项:
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'];
const result = story.replace(new RegExp(
"(" + overusedWords.concat(unnecessaryWords).join("|") + ")[^a-zA-Z]*", "gi"
), "");
console.log(result);