How to fix “Array.prototype.filter() expects a value to be returned at the end of arrow function”

I’m having some issues understanding why I’m getting a warning on this piece of my React code

Array.prototype.filter() expects a value to be returned at the end of arrow function

but it seems to work.

 {customers
    .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
    .filter((customer) => {
      if (searchTerm === "") {
        return customer;
      } else if (
        customer.cust_main_name.toLowerCase().includes(searchTerm.toLowerCase())
      ) {
        return customer;
      }
    })

What changes do I need to implement to avoid this warning?

Answer

Array.prototype.filter expects a value to be returned from within the callback function implying whether to keep or rejeect the current itemm value

However in your callback function you have if-else-if block where you return thee value but it none of the if or else-if conditions match, you don’t return anything which what the error points

You can return false if none of your if or else-if condition matches

customers
    .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
    .filter((customer) => {
      if (searchTerm === "") {
        return customer;
      } else if (
        customer.cust_main_name.toLowerCase().includes(searchTerm.toLowerCase())
      ) {
        return customer;
      }
      return false;
    })