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

问题描述

正在学习 JavaScript,目前在迭代器上做一个项目来“lint”一个有文本故事的数组。 linting 指的是从某种形式的写作中编辑/过滤掉单词或语法的过程。

我的问题 - 目前我可以将其过滤为 .filter overusedWords,但我想保存一个单词的使用次数并返回该数量,例如:(“您使用了 ${overusedWords} X 次”)。但是它只会打印我正在过滤的单词,无论它出现多少次。

我尝试在这个迭代器循环内/外定义字计数器。现在我正在尝试将该部分重写为 'if (overused.filter(word => { === 'example'}) type

该项目是为了习惯 .(iterator) 方法,所以解决方案应该围绕使用它。希望你能帮我解决这个问题并更好地学习:)

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,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,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'}.`);
});