filter with content text

0

I use this statement to perform a filtering on a JSON object variable:

var a = $(Data2).contains(function (index, value) { 
    return value.ApPaterno === Apellido; 
});

It works correctly but as long as the text is exactly the same, is there a way to evaluate something like SQL Like ???

    
asked by EriK 31.08.2017 в 17:16
source

2 answers

1

The .indexOf() method that returns the position of the first word that is equal to the argument:

var indice = "hola mundo, como estas".indexOf("com");
console.log(indice > 0 ? "contiene la letra com" : "no contiene la letra com");

var indice2 = "hola mundo, como estas".indexOf("bue");
console.log(indice2 > 0 ? "contiene la letra bue" : "no contiene la letra bue");

Then in your case it would be:

var a = $(Data2).contains(function (index, value) { 
    return value.ApPaterno.toLowerCase().indexOf(Apellido.toLowerCase()) > 0;
});

If you want to filter the con jquery, use the function grep() that filters the array that complies with The specified condition:

var data = [
  {ApPaterno: "Martines", nombre: "Carlos"},
  {ApPaterno: "Martones", nombre: "Mateo"},
  {ApPaterno: "Maruecos", nombre: "Handel"}
];
var apellido = "nes";
var coleccionFiltrada = $.grep(data,function(persona, index){ 
  
   return persona.ApPaterno.indexOf(apellido) > 0;
});

console.log(coleccionFiltrada)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

And if you want to do it without jquery, then use the filter function of the array:

 var data = [
      {ApPaterno: "Martines", nombre: "Carlos"},
      {ApPaterno: "Martones", nombre: "Mateo"},
      {ApPaterno: "Maruecos", nombre: "Handel"}
    ];
    var apellido = "nes";
    
 var datosFiltrados = data.filter(function(persona, index){ 
    return persona.ApPaterno.indexOf(apellido) > 0; 
 });
 
 console.log(datosFiltrados);
    
answered by 31.08.2017 в 17:24
0

You could try this:

var a = $(Data2).contains(function (index, value) { 
    return (value.ApPaterno.toLowerCase().indexOf(Apellido) >= 0)
});

indexOf() returns -1 if it does not find the string .

    
answered by 31.08.2017 в 17:24