Filter list by keyword array

Asked

Viewed 30 times

-2

The idea is to make a filter.

I have a text field that the user types what they want to filter, so I store the typed words in an array. Ex.: The user typed in the "config company" field, the array looks like this:

arrayBusca = ['config', 'empresa']

I need to filter the list so it displays only items it contains BOTH array items. If the user type "company config" must display the record "Company setup" and hide the others as "Company" or "General settings".

So far I did this, but it doesn’t work because it displays other records that contains only the word "Config" and others that only contains "company"

var arrayBusca = busca.split(" ");

for(x in searchArray){
    menusFiltrados = $('.texto:contains(\'' + arrayBusca[x].trim() + '\')')
}

1 answer

-1


You can "concatenate" several "contains". Example: $('.texto:contains("config"):contains("empresa")')

You can make a loop that adds the contains, I made one just for example, you can improve.

var arrayBusca = ["config", "empresa"];
var stringBusca = '';

arrayBusca.forEach((item) => stringBusca = stringBusca.concat(`:contains("${item}")`));

console.log(stringBusca); // output: :contains("config"):contains("empresa")

I got the answer to a question from the OR in English, I can not only comment on the low reputation. https://stackoverflow.com/questions/2416803/jquery-contains-selector-to-search-for-multiple-strings

  • Very good! That’s what I needed, I repeated the question in the English OS if you want to take a look! https://stackoverflow.com/questions/67403462/how-to-filter-a-list-using-an-array-of-keywords/67403493?noredirect=1#comment119142363_67403493

Browser other questions tagged

You are not signed in. Login or sign up in order to post.