Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript filtering objects and Arrays

I’m trying to learn javascript by following a tutorial on youtube, and I found this segment of code.

I understand what filter generally does or at least its purpose, but I don’t know anything about filter conditions what they do exactly? Any explanation will be appreciated.

setFilteredProducts(
    products.filter((item) =>
        Object.entries(filters).every(([key, value]) =>
            item[key].includes(value)
        )
    )
);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.entries(filters)

basically returns an array iterator object which is then being iterated using

.every(([key, value])

This is basically creating an iterator(entries) and iterating(every) through it.
Finally, .includes(value) checks if the value is present in item[key]. It is basically a function to search for an element.

Hope it helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading