I am trying to compare an array of elements with the elements of a 2D array. If there is a match found, then the count for that row of 2D elements will increase. I managed to do it with the first row of 2D array however I do not know how to make the code keep checking for the next row of the 2D array.
var fruits=[
['apple', 'banana', 'mango'],
['grape', 'pineapple', 'blueberry'],
['strawberry', 'mangosteen']
];
var fruit_i_like=[
['grape', 'banana', 'pineapple']
];
//if found match from the first row of fruits, increment this var
var fruit1_count = 0;
//if found match from the second row of fruits, increment this var
var fruit2_count = 0;
//if found match from the third row of fruits, increment this var
var fruit3_count = 0;
for (var i = 0; i < fruit_i_like.length; i++) {
for (var j = 0; j < fruits.length; j++){
if (fruits[j].indexOf(fruit_i_like[i]) > -1) {
fruit1_count++;
}
}
}
The expected result should be printing the number of match the fruit_i_like array has with every rows of the array fruits. For example, here fruit1_count would be 1, fruit2_count would be 2, fruit3_count would be 0.
Is there any way of checking the other rows, using pure JS? Thank you!