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);
Where did it fit into another array? in a string? It’s not very clear to me what you’re trying to do.
– BrTkCa
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
– Leo Letto
https://3v4l.org/l7efb < this script gives error
– MoshMage
@Moshmage If you run the function without creating the variables that make it validated, it will give error anyway, try https://3v4l.org/K5YNt
– Leo Letto