1

I have this function with jquery, but I need it to work like operator "like" Help me. Thank you very much for your help

works correctly if I look the whole word example

var vsearch = "home"  
var array = _.filter(objeto, function(product){ 
return product.filtro ==vsearch;

var vsearch=`H`
var array = _.filter(objeto, function(product){ 
return product.filtro ==vsearch;

No looks for containing the H

Thanks.

Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85
Daniel
  • 21
  • 1
  • 5

2 Answers2

3

Use indexOf, which returns -1 if a string is not found, to see if the product contains the letter you're searching for.

var vsearch=`H`;
var array = _.filter(objeto, function(product){ 
   return product.filtro.indexOf(vsearch) > -1;
}
Ryan Erdmann
  • 1,786
  • 10
  • 11
0

You could try something like this:

var stringToSearch = "The full string to search goes here";
var searchTermContains = stringToSearch.search("H");

This will return a -1 if no match is found.

mwilson
  • 12,295
  • 7
  • 55
  • 95