Javascript 使用.map和store创建过滤器

Javascript 使用.map和store创建过滤器,javascript,reactjs,filter,store,Javascript,Reactjs,Filter,Store,我正试着为我的餐馆做一个过滤器。代码在没有过滤器的情况下工作,但是当我尝试添加过滤器时,我得到以下错误:TypeError:restaurant.toLowerCase不是函数|第25行 当我使用 const list=['jef','uwmoder']和list.map 它确实有效,但与商店有关。没有 有人能看一下吗 <ul> {store.restaurants.map(restaurant => { if (filter.leng

我正试着为我的餐馆做一个过滤器。代码在没有过滤器的情况下工作,但是当我尝试添加过滤器时,我得到以下错误:TypeError:restaurant.toLowerCase不是函数|第25行

当我使用

const list=['jef','uwmoder']
list.map
它确实有效,但与商店有关。没有

有人能看一下吗

   <ul>
        {store.restaurants.map(restaurant => {
          if (filter.lenght !== 0) {
            if (restaurant.toLowerCase().startsWith(filter.toLowerCase())) {
              return (
                <li>
                  <Restaurant
                    restaurant={restaurant}
                    key={restaurant.id}
                    onClick={() => {
                      uiStore.setCurrentRestaurant(restaurant);
                      uiStore.setCurrentView(VIEWS.detail);
                    }}
                  />
                </li>
              );
            } else {
              return null;
            }
          }
          return (
            <li>
              <Restaurant
                restaurant={restaurant}
                key={restaurant.id}
                onClick={() => {
                  uiStore.setCurrentRestaurant(restaurant);
                  uiStore.setCurrentView(VIEWS.detail);
                }}
              />
            </li>
          );
        })}
      </ul>
    {store.restaurants.map(restaurant=>{ 如果(filter.lenght!==0){ if(restaurant.toLowerCase().startsWith(filter.toLowerCase())){ 返回(
  • { uiStore.setCurrentRestaurant(餐厅); uiStore.setCurrentView(VIEWS.detail); }} />
  • ); }否则{ 返回null; } } 返回(
  • { uiStore.setCurrentRestaurant(餐厅); uiStore.setCurrentView(VIEWS.detail); }} />
  • ); })}
这是餐厅的组成部分:

const Restaurant = ({restaurant, onClick}) => {
  return useObserver(() => (
    <div className={style.restaurantlist} >
      <Link to={`/detail/${restaurant.id}`}>
      <img onClick={onClick} className={style.restaurantfoto} src={restaurant.picture} alt={restaurant.name} />
      </Link>

      <section className={style.information}>
        <Information restaurant={restaurant} />
      </section>


    </div>
  ));
}
const Restaurant=({Restaurant,onClick})=>{
返回useObserver(()=>(
));
}

您需要在问题本身中包含所有必要的信息(),而不仅仅是在其他地方。
filter.lengh
?这是
过滤器
对象上的有效属性吗?映射回调中餐厅的值或对象类型是什么?如果它不是字符串,那么错误就有意义。根据后面在
餐厅中的用法,
似乎是一个对象,而不是字符串。@drewerese我编辑了这篇文章并添加了
餐厅
组件well,这是一个非常有用的proptype定义。也可以是
PropTypes。任何
或什么都没有。那么,您试图
toLowerCase()
做什么呢?请删除
调试器
控制台日志(餐厅)restaurant.toLowerCase()
并检查
restaurant
的实际对象形状之前,请使用控制台日志的输出更新问题,以便我们可以查看
restaurant
对象的实际形状。或者分享
store.restaurants
的形状。