Filter the database loaded variable in javascript

Asked

Viewed 115 times

0

I have a variable, which brings me all the data from the database.
There is how, in javascript, I make a filter on it still, like it brings me 10 tuples and by the filter only one.
Before anyone tells you to do this at the bank, I’d like to know if you can do this in javascript, just for learning.

Edit:

As it was not clear, I will clarify.
There is how to filter a recordset with javascript?
Sérgio answered me by array and filter.
The question was this. Is there any way to filter a recordset using pure javascript? Classic Asp Recordset.

  • I didn’t get it right, but if you have a variable with database data, you can easily make comparisons using var.nome_coluna: if (pessoa.cpf.length != 11), for example.

1 answer

2

The answer is yes.

It depends a little on how you have the data but the function .filter() works with Arrays. If you have an array everything ok, otherwise you have to use Object.keys(teuObjeto).filter( and iterate the keys of that object.

And how to filter?

The function that is passed to the method .filter() must have a return. If this Return gives true result (Boolean value) then this element is, if false then it is deleted.

Example;

var arrayExemplo = [1, 2, 3, 'foo'];

var novaArray = arrayExemplo.filter(function(elemento, index){
    return typeof elemento == 'string';
});

Upshot:

console.log(novaArray); // dá ['foo']

That is, it has filtered the elements of the array that nay are of the type String.

Another radical example would be:

var novaArray = arrayExemplo.filter(function(elemento, index){
    return false;
});

This example gives an empty array ([]) because all the results are false.

  • I have a recordset coming from Oracle. I need to filter by two values that I have. Cod_provider and Tipo_provider, for example. I consider recordset as an array? I don’t think so.

  • @pnet can’t answer without seeing the code. You get this by ajax? what gives console.log(dadosDoOracle);?

Browser other questions tagged

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