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)
)
)
);
>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!