0

How can I count how many times wordToCount are in the phrasesToCheck. And how to put this number to counter variable ?

let counter = [];
let wordToCount = ["tomato","cat"];
let phrasesToCheck = ['my cat like potatoes','cat like apple','my golden fish like tomato'];
counter[0] = 1; //tomato
counter[1] = 2; //cat
pawel szewczyk
  • 261
  • 2
  • 11

1 Answers1

0

let wordToCount = ["tomato","cat"];
let phrasesToCheck = ['my cat like potatoes','my cat like apple','my golden fish like tomato'];
let allwords = phrasesToCheck.join(' ').split(' ');
let counter = wordToCount.map(word => allwords.reduce((acc, v) => acc += v === word, 0));
console.log(counter);
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87