2

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!

    3 Answers 3

    5

    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); 
    3
    • It works well but just an FYI: it will loop over every combination for matches. For instance during the first loop: "Banana" will match the first element (and the second) after which on the loop for "Apple" it will try to match those two elements again. This may or may not be a bad thing (maybe you want to push the specified fruit onto the output array twice in this example) but just something to note in case this isn't what @baki intended.CommentedJul 1, 2015 at 19:08
    • 1
      @JasonCust yes that is true it's assuming he's not going to have a "Big Banana Apple" If he does have such a case then it would get put in the array twice maybe that's what he wants maybe not...Going on the assumption that will never happen.
      – brso05
      CommentedJul 1, 2015 at 19:13
    • Agreed, hence the second part of the comment. :)CommentedJul 1, 2015 at 19:14
    3

    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>";

      0

      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>');

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.