rob.m
rob.m

Reputation: 10571

How to check if any word inside a text is in array?

Given a whole text like:

var nation = "Piazza delle Medaglie d'Oro
40121 Bologna
Italy"

And a given array like:

 ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]

How can we check that the word Italy within that whole text is in the array?

Following this SO answer this is what I tried, but I get False while instead Italy is present within the array

  var countries = [];
  $("#usp-custom-3 option").each(function() {
    var single = $(this).text();
    countries.push(single);
    var foundPresent = countries.includes("Piazza delle Medaglie d'Oro 40121 Bologna Italy");
    console.log(foundPresent); 
  });

JsFiddle here

Upvotes: 2

Views: 834

Answers (5)

Jonas Wilms
Jonas Wilms

Reputation: 138247

If you check whenever you push to an array, its even much simpler, just check the pushed element:

const text = " I like Italy";
const nations=[];

function insert(single){
 if( text.includes(single) /*may format single, e.g. .trim() etc*/){
   alert("Nation in text!");
 }
 nations.push(single);
}

Run


If you still want to check the whole array everytime, a nested iteration may does it:

let countries = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
const text = " I like Italy";

let countriesInText = countries.filter( word => text.includes( word ) );
//["Italy"]

Run

Performance compared to Rajeshs answer

If you just care if or if not, may use .some() instead of .filter().

Upvotes: 5

Neeraj Pathak
Neeraj Pathak

Reputation: 759

$(function () {

    try {
        var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";

        var I = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]

        for (var index = 0; index < I.length; index++) {
            if (nation.toString().toUpperCase().indexOf(I[index].toString().toUpperCase()) >= 0) {
                console.log(I[index].toString());
            }
        }
    }
    catch (err) {
        console.log(err);
    }
});

try this one.

Upvotes: 0

Rajesh
Rajesh

Reputation: 24915

Since you need to search a string with words in an array, best option is to use a regex and use string.match(regex) to get the matched words.

var nation = `Piazza delle Medaglie d'Oro
40121 Bologna
Italy`;
//var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
var countries = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
var regex = new RegExp(countries.join("|"), "i");
console.log(nation.match(regex))

Upvotes: 3

ricristian
ricristian

Reputation: 536

this script will compare every word from var nation with all elements of array that you provided. I hope that this will solve your issue

<script>
    var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
    test = nation.split(" ");
    array = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
    test.forEach(function (element) {
        array.forEach(function (array_to_compare) {
            if (element == array_to_compare)
                alert("We found that word " + element + " matches in array");
        });
    }, this);
    each()

</script>

Upvotes: 0

Ankur
Ankur

Reputation: 181

var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
searchStringInArray("Italy", nation);
function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}

Upvotes: 0

Related Questions