【问题标题】:JS - how to count (filtered) words as you iterate through an arrayJS - 如何在遍历数组时计算(过滤的)单词
【发布时间】:2021-08-04 01:04:37
【问题描述】:

正在学习 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, 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);

【问题讨论】:

    标签: javascript arrays iterator filtering


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-27
      • 2017-09-03
      • 2021-11-28
      • 2015-12-21
      • 2015-12-29
      • 2021-10-13
      • 2019-12-19
      • 1970-01-01
      相关资源
      最近更新 更多