Skip to content
Advertisement

Filter html elements based on data attribute

I have the following html structure

JavaScript

And what I want to do is to get the count of child divs that contains different data attribute. For example, I’m trying this:

JavaScript

But that code is returning 6 and what I want to retrieve is 4, based on the different data-customId. So my question is, how can I add a filter/map to that selector that I already have but taking into consideration that is a data-attribute.

I was trying to do something like this:

JavaScript

Advertisement

Answer

After you getting the child-divs map their customid and just get the length of unique values:

JavaScript
JavaScript

Note: $ is equivalent to document.querySelectorAll in js returns a NodeList that’s why I destructure it by the three dots ...

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement