How to count elements by class using list.js

155 Views Asked by At

I am using list.js for pagination and I am trying to count items in list by category. Here i have a list 7 people, of which : 1 is born in 1970, 2 are born in 1983, and 4 are born 1993. So i am trying to count only people born in 1993 and append the result to a span element. I have only managed to count all items/people in the list using:

var initDB = userList.matchingItems.length;

document.getElementById("iDatabase").innerHTML = initDB;

can you please help

1

There are 1 best solutions below

0
jlogan On

You can filter userList.matchingItems using JS's filter array method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Or you can loop over userList.matchingItems and count the ones born in 1993.