Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React/JavaScript: Where does "item" come from in this function?

This is a function from a book that I am working through:

function byQuery(query) {
    return function(item) {
        return !query ||
            item.name.toLowerCase().includes(query.toLowerCase());
    }
}

Its purpose is to filter a list of objects--but I am not sure where item is coming from--its only use (in the given code) is like so:

<List list={(list || []).filter(byQuery(query))} />
like image 208
user8951490 Avatar asked Jul 10 '26 00:07

user8951490


1 Answers

item comes from the argument that filter provides to its callback function. byQuery(query) returns a function is basically the function that is used by filter. It would be similar to this

{(list || []).filter(function(item) {
    return !query ||
        item.name.toLowerCase().includes(query.toLowerCase());
})}
like image 88
Shubham Khatri Avatar answered Jul 11 '26 13:07

Shubham Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!