JS-如何在遍历数组时计算(过滤的)单词



学习JavaScript,目前在迭代器中执行一个项目来"lint"一个具有文本故事的数组。临摹是指从某种形式的写作中编辑/过滤掉单词或语法的过程。

我的问题-目前我可以将其设置为。过滤过度使用的单词,但我想保存一个单词的使用次数,并返回该次数,如:("您使用了${overusedWords}X次"(。然而,无论出现多少次,它都只打印我正在过滤的单词。

我已经尝试在这个迭代器循环内部/外部定义单词计数器。现在我正试图将该部分重写为"if"(过度使用.filter(word=>{==="example"}(类型的

这个项目是为了适应。(迭代器(方法,所以解决方案应该围绕着使用它。希望你能帮助我弄清楚这一点,并更好地学习:(

let word1 = 0;
let word2 = 0;
let word3 = 0;
const countOverUsed = betterWords.filter((word, word1, word2, word3) => {
if (word === 'really') {
word1 = word1 + 1;
return word1;
} else if (word === 'very') {
word2 = word2 + 1;
return word2;
} else if (word === 'basically') {
word3 = word3 + 1;
return word3;
}
});
console.log(countOverUsed);

整个项目

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 storyWords = story.split(' ');
console.log(storyWords.length);
const betterWords = storyWords.filter(function(element) {
if (unnecessaryWords.includes(element)) {
console.log(element)
} else {
return element;
}
});

let word1 = 0;
let word2 = 0;
let word3 = 0;
const countOverUsed = betterWords.filter((word, word1, word2, word3) => {
if (word === 'really') {
word1 = word1 + 1;
return word1;
} else if (word === 'very') {
word2 = word2 + 1;
return word2;
} else if (word === 'basically') {
word3 = word3 + 1;
return word3;
}
});
console.log(countOverUsed);

Array.prototype.filter()返回一个与函数内的筛选条件匹配的数组。根据布尔返回值,它是否将当前元素添加到返回值中。

[1,2,3,4,5,6].filter((value) => {
return value % 2
}); // returns: [1, 3, 5]

如果要迭代数组的每个元素,请使用任何类型的循环或Array.prototype.forEach

您可以使用多种方法来搜索文本中的单词。

以下是String.prototype.indexOf()的一个示例。

let overusedWordsCount = {};
overusedWords.forEach((word) => {
overusedWordsCount[word] = 0;
for (let position = 0;story.indexOf(word, position) > -1; overusedWordsCount[word]++) {
position = story.indexOf(word, position) + word.length;
}
});
Object.entries(overusedWordsCount).forEach(([ word, value ]) => {
console.log(`You used ${word} ${value} time${value == 1 ? '' : 's'}.`);
});

最新更新