How to simulate this PHP function in jQuery?

Asked

Viewed 76 times

2

The function below made in PHP will return me an array where the searched word fits, as I can do the same though with jQuery?

$meuArrayMultidimensional = array(array("campo" => "teste"), array("campo" => "valor qualquer"), array("campo" => "teste"));
$search = "uma palavra qualquer";
$retorno = array_keys(
    array_filter(
    $meuArrayMultidimensional,
        function ($value) use ($search) {
            return (stripos($value['colunaDoArray'], $search) !== false);
        }
    )
);
  • Where did it fit into another array? in a string? It’s not very clear to me what you’re trying to do.

  • Return will fetch the myArray in the specified column the string received in the $search variable, and will add the keys where this string was found in the array, turning the $return into an array of keys

  • https://3v4l.org/l7efb < this script gives error

  • @Moshmage If you run the function without creating the variables that make it validated, it will give error anyway, try https://3v4l.org/K5YNt

1 answer

1


If the goal is to filter to get the field later, you can use the array.filter() -- if it is to take the index, you can use the array.forEach()

the equivalent of stripos is "string".indexOf() and you can’t hit against false because he returns 0 if the index found is at position 0 of the string. you can also use the search - if you need to use Regex (Pex "hello world".search(/\sworld/i))

To iterate over an object, you can iterate through its keys with the Object.keys():

var objectoMultidimensional = {'0':{campo: 'teste'},'1':{campo: 'valor qualquer'}, '2':{campo: 'teste'}}
var result3 = [], objecto;
Object.keys(objectoMultidimensional).forEach(function(key, index) {
   objecto = objectoMultidimensional[key]; 
   if (objecto.campo.indexOf(search) > -1) {
       result3.push(objecto)
   }
});

console.log(result3)

you can still use the Object.values allied with the array.filter if the browser supports it (or if you are using a Shim):

var result4 = Object.values(objectoMultidimensional).filter(function(objecto) { 
    return objecto.campo.indexOf(search) > -1 
})

console.log(result4)

How do you notice, Object.values() returns an array with the values within the given object, instead of Object.keys() only returns keys from it.

To do the same with the search() that I talked about:

var search = 'TESTE';
var regExp = new RegExp(search, 'i');
var resultado = ['olá', 'mundo', 'teste'].filter(function(text) { return text.search(regExp) > -1 });
console.log(resultado);
  • I tested it here and it didn’t work with my original array, which is in this format, how do I do? : http://imgur.com/a/ROSwz

  • 1

    @Leoletto edited to match your output :)

  • result4 worked perfectly for me, but there’s no way to simulate strips? Because the function only returns data when I type the result exactly as it is in the array, and as it is to be a search system, I would like it to be a little more malleable with respect to that

  • Opa already found the solution here putting toLowerCase() before the index, I don’t know if it’s right but apparently it worked, thanks for the help @Moshmage

  • 1

    @Leoletto my fault: I forgot that the strIpos is case-insensitive; in this case, uses .search() instead of indexOf - but you’ll have to make a regular statement before the search word; I’ve edited the answer to have that example too

  • Turning everything into minuscule letter already worked for me, would it be necessary to change? kk is that I’ve re-structured everything here to take the interaction with php

  • 1

    Nope, it’s just one more you can use :) Edit: as long as it doesn’t make a difference in your output/input, the Regexp-free index is even faster)

Show 2 more comments

Browser other questions tagged

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