Reputation: 614
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!
Upvotes: 2
Views: 7466
Reputation: 10909
On modern browsers and NodeJS you could use a filter
to test the fruitsSpecified
array items using some
and indexOf
. The main benefit here is that you loop over the array to search only once and then if any search terms match the rest of the search terms are skipped for that search loop.
var fruits = ["Banana", "Apple"];
var fruitsSpecified = ["Big Banana", "Banana Small", "Black Apple", "Big Orange"];
function testFunction(arr, searchArr) {
return arr.filter(function(item) {
return searchArr.some(function(searchTerm) {
return item.indexOf(searchTerm) > -1;
});
});
}
var newArray = testFunction(fruitsSpecified, fruits);
document.write('<pre>' + JSON.stringify(newArray) + '</pre>');
Upvotes: 0
Reputation:
Pretty simple with .filter()
, .some()
and .indexOf()
.
var result = fruitsSpecified.filter(function(fs) {
return fruits.some(function(ff) { return fs.indexOf(ff) > -1 });
});
var fruits = ["Banana", "Apple"];
var fruitsSpecified = ["Big Banana", "Banana Small", "Black Apple", "Big Orange"];
var result = fruitsSpecified.filter(function(fs) {
return fruits.some(function(ff) { return fs.indexOf(ff) > -1 });
});
document.body.innerHTML = "<pre>" + JSON.stringify(result, null, 2) + "</pre>";
Upvotes: 3
Reputation: 13232
You can loop on your first array and see if each element of the second array contains the current element of your first array:
function testFunction() {
var newArray = [];
for (var i = 0; i < fruits.length; i++) {
for(var j = 0; j < fruitsSpecified.length; j++)
{
if(fruitsSpecified[j].indexOf(fruits[i]) != -1)
{
newArray.push(fruitsSpecified[j]);
}
}
}
console.log(newArray);
}
Console Output:
["Big Banana", "Banana Small", "Black Apple"]
If you want you can make it more generic by passing the arrays as parameters:
function testFunction(fruits, fruitsSpecified) {
var newArray = [];
for (var i = 0; i < fruits.length; i++) {
for(var j = 0; j < fruitsSpecified.length; j++)
{
if(fruitsSpecified[j].indexOf(fruits[i]) != -1)
{
newArray.push(fruitsSpecified[j]);
}
}
}
console.log(newArray);
return newArray;
}
You can call it like this:
var fruits = ["Banana", "Apple"];
var fruitsSpecified = ["Big Banana", "Banana Small", "Black Apple", "Big Orange"];
var newArray = testFunction(fruits, fruitsSpecified);
console.log(newArray);
Upvotes: 5