JSFiddle - React, Tailwind, and code Playground
HTML
<div class="output"></div>
<div class="result"></div>
JavaScript
var groups = [
{name: 'fruits', words:["apple","strawberry", "banana"]},
{name: 'test', words:["asd","qwe"]}
];
var searchwords = ["apple", "strawberry"];
// Initialize empty array
var res = Array(groups.length).fill(0);
// Initialize a counter
var biggestIndex;
var biggestValue = 0;
for(i = 0; i < groups.length; i++) {
for(j = 0; j < groups[i].words.length; j++) {
if(searchwords.includes(groups[i].words[j]) == true){
res[i] += 1;
if(res[i] > biggestValue){
biggestValue = res[i];
biggestIndex = i;
}
}
}
}
// biggestIndex holds the index with the most matches
document.querySelector(".output").textContent = "The best group is \"" + groups[biggestIndex].name + "\" group";
// Res holds the list output
document.querySelector(".result").textContent = res;