I'll get straight to the point. I have two arrays:
fruits = ["Banana", "Apple"]; fruitsSpecified = ["Big Banana", "Banana Small", "Black Apple", "Big Orange"];
The result I'm looking for:
newArray = ["Big Banana", "Banana Small", "Black Apple"];
So I want to check if fruitsSpecified
strings contain part of fruits
and put those results into a new array. How do I do that? All the answers I found were over-complicated and looked only for exact matches (e.g. "Apple" in array#1 and "Apple" in array#2).
I have this so far:
function testFunction() { newArray = []; for (i = 0; i < fruitsSpecified.length; i++) { myResult = fruitsSpecified.indexOf(fruits[i]); newArray.push(myResult); } console.log(newArray); }
which obviously does not work since it only finds exact matches.
I have checked these questions (but found them too complicated, I believe/hope there is a simpler solution):
Best way to find if an item is in a JavaScript array?
How do I check if an array includes an object in JavaScript?
Thanks!