简体   繁体   中英

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

Array.prototype.filter() expects a value to be returned at the end of arrow function.I am getting this error

    {product
          .filter((item) => {
            if (search.toString().toLowerCase()) {
              return item;
            } else if (
              item.name.toLowerCase().includes(search.toString().toLowerCase())
            ) {
              return item;
            }
          })
          

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

However in your callback function you have if-else-if block where you return the value but if 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 Try below:

 {product
              .filter((item) => {
                if (search.toString().toLowerCase()) {
                  return item;
                } else if ( 
 item.name.toLowerCase().includes(search.toString().toLowerCase())
                ) {
                  return item;
                }
               return false;
              })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM