I have an Array Data
:
var Data = [{"Nombre": "A","Apellido": "B","Edad": 12},{},{},{},...]
How can I filter the data so that all those with Apellido
are equal to B
, for example?
I have an Array Data
:
var Data = [{"Nombre": "A","Apellido": "B","Edad": 12},{},{},{},...]
How can I filter the data so that all those with Apellido
are equal to B
, for example?
From the following code:
var Data = [{"Nombre": "AA","Apellido": "B","Edad": 12},{"Nombre": "BA","Apellido": "C","Edad": 12},{"Nombre": "CA","Apellido": "D","Edad": 12},{"Nombre": "DA","Apellido": "E","Edad": 12},{"Nombre": "EA","Apellido": "F","Edad": 12},{"Nombre": "GA","Apellido": "G","Edad": 12},{"Nombre": "HA","Apellido": "B","Edad": 12},{"Nombre": "IA","Apellido": "B","Edad": 12},{"Nombre": "JA","Apellido": "B","Edad": 12}];
var filtro = $(Data).filter(function (index,element){return element.Apellido==='B';});
for (var i=0;i<filtro.length;i++)
{
console.log(filtro[i].Nombre +" "+filtro[i].Apellido)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(Data)
makes the Data variable an object of jQuery
.
.filter
is a function of jQuery
, direct from its official website: link says the following:
Description: Reduce the set of matched elements to those that match the selector or pass the function's test.
Interpreted to good Spanish:
Description: Reduces the set of elements that have the same selection pattern.
In this case, we use function(index, element)
the documentation says:
A function used as a test for each element in the set. this is the current DOM element
Interpretation to good Spanish:
A function is used as a test for each element in the set "this" is the current DOM element.
In this case, each element
represents {"Nombre": "AA","Apellido": "B","Edad": 12}
and how we need to obtain that element that has its property (Surname) equal to "B": return element.Apellido==='B';
Now, the filter variable is an array that contains the new set of filtered elements (worth the redundancy).
A for loop allows us to iterate through all the elements and finally we show them with a console.log
.
Hello you can use Array.filter
is native to JavaScript was added in the ECMAScript5 specification so it is supported by almost all browsers today.
For your particular problem it is quite simple, but if you simplify it with the new notation:
let resultado = Data.filter(obj => obj.Apellido === 'B');
I invite you to also try Array.find
, which is similar but returns the first element that matches the filter in case none match will return undefined
.
Note : Recommendation starts using let
instead of var
in order to have more control over the scope of the variables you define. You can read more about let
.
There are many ways to filter that Array of objects data
, for example:
var Data = [{"Nombre": "A","Apellido": "B","Edad": 12}];
var contenidoFiltrado = Data.filter((x) => (x.Apellido[0] === 'B'));
The variable contenidoFiltrado
contains all the elements of the Array data
, whose Apellido
starts with B
.