0

I am new in JS and need some help. In this example: I have a List, in this list are "keywords" in an array.

I have found some links, but I don't know how to put this into my script right now..

find String of an Array that is in an array

Javascript: Search for an array in an array of arrays

-> So I want, when I search ["apple", "strawberry"] that the script outputs the most matched list.

This was my idea, I tried many things.. but nothing worked out like I was supposed to.


var groups = [
    {name: 'fruits', words:["apple","strawberry", "banana"]},
    {name: 'test', words:["asd","qwe"]}
];

var searchwords = ["apple", "strawberry"];

for(i = 0; i < groups.length; i++) {
    console.log(groups[i]);
    for(i = 0; i < searchwords.length; i++) {
        console.log(searchwords[i]);
        console.log('The most matching list is: ' + groups.name);
    }
}

OUTPUT should be:

The best group is "fruits" group.

And I want a list (output), like that:

result = [2,0]

The "2" is for the List "fruits", because there are 2 words in it. The "0" is for the List "test", because there no matching words.

Thank you!

  • Oh and this link is also helpful.. but there's just one property https://stackoverflow.com/questions/6237537/finding-matching-objects-in-an-array-of-objects – Dave Reiß Apr 12 '20 at 19:54

3 Answers3

1

I won't give you the full answer but here's what you should be doing.

for(i = 0; i < groups.length; i++){

}

This first part is correct! You will be iterating over each group. Now what you need to be doing next is to be iterating over each fruit inside the current group.

for(i = 0; i < groups.length; i++){
    for(j = 0; j < groups[i].words.length; ++j){

    }
}

This will give you access to each fruit inside the current group. Now, you need to find a way to count how many times a fruit has been repeated; maybe you should create a new dictionary for that.

Try and do it by yourself, if you find any other issues, let me know.

kibe
  • 90
  • 1
  • 8
  • 26
0

Try it on your own using the clues kibe gave first. This is one way you can do it, I tried to keep your original code as much as possible and wrote the outputs you wanted. Hope it helps! Let me know if something doesn't make sense/if you need an explanation of anything.

var groups = [
    {name: 'fruits', words:["apple","strawberry", "banana"]},
    {name: 'test', words:["asd","qwe"]}
];

var searchwords = ["apple", "strawberry"];

// Initialize empty array with size equal to size of groups
var res = Array(groups.length).fill(0);
// Initialize variables to hold biggest number of matches and index of said matches
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
console.log("The best group is", groups[biggestIndex].name, "group");
// Res holds the list output you wanted
console.log(res);

https://jsfiddle.net/w9nqr02m/

WildWombat
  • 396
  • 1
  • 13
0

There are quite some ways to do it. One I would prefer:

const result = groups.map(group => {
    const matchedwords = group.words.filter(word => searchwords.includes(word));
    return matchedwords.length;
}

PS: Follow kibe's answer to try to solve the way you were doing. I would also recommend you to see map and filter documentation if you're not familiar with them. They're quite handy while working with arrays.