Assuming I have an array of JSON that looks like this:
[
{
'nombre': 'X',
'asistio': true
},
{
'nombre': 'X',
'asistio': false
},
{
'nombre': 'X',
'asistio': true
},
{
'nombre': 'X',
'asistio': true
}
]
How could I count elements whose key asistio === true
?
It can
.filter
also be used, which makes it a bit shorter, since we can take advantage of the fact that itasistio
is true or false :You just have to iterate through the array and count like this:
I would use reduce:
I tell you that you can also use
map()
, which when iterating returns a new array and will show you the result you expect; it's just a matter of looking at itI also tell you that as the value you are looking to count how many times these are boolean; it is enough with the syntax like data.assisted since by default it will search first for those values that are true, you can also use the
if
ternary syntax that has its structure in this way:(variable) ? valorAfirmatvo : valorFalso
as followsFor more information about the map() method as well as examples that give you more clarity on how it works, I leave you reference documentation