【问题标题】:How to search for anything from an array of objects,?如何从对象数组中搜索任何内容?
【发布时间】:2022-09-23 17:32:53
【问题描述】:

如何从对象数组中搜索任何内容?

 let cars = [
    {
      \"color\": \"purple\",
      \"type\": \"minivan\",
      \"registration\": new Date(\'2017-01-03\'),
      \"capacity\": 7
    },
    {
      \"color\": \"red\",
      \"type\": \"station wagon\",
      \"registration\": new Date(\'2018-03-03\'),
      \"capacity\": 5
    },
 
  ]


 return (
  <>

     <input type=\"text\" onChnage={(e) =>setSearchInput(e.target.value)}
    {
      cars?.filter(x => Object.values(x).includes(searchInput.toLowerCase())).map((d) =>{
         console.log(d) // nothing is consoled from here tu
        //show the data here  but nothing is showing here 
        
      })
    }

  </>
 )

没有错误,但数据没有显示任何内容,这里有什么问题?

  • Object.values(x).includes(searchInput.toLowerCase()) 将返回布尔值。因此地图不会被执行
  • @SrushtiShah那么我需要改变什么才能得到我想要的?
  • 请发布您的整个代码,以便我可以帮助您
  • @死人...\“没有错误,但数据没有显示任何内容,这里有什么问题?\”......也许是一个错字? ... onChnage 应该是 onChange

标签: javascript reactjs arrays filter jsx


【解决方案1】:

Object.values(x).includes(searchInput.toLowerCase()) 将返回布尔值。因此地图不会被执行

更改您的返回语句,如下所示

return (
  <>

     <input type="text" onChnage={(e) =>setSearchInput(e.target.value)}
    {
      cars?.map((x) => {
         Object.values(x).includes(searchInput.toLowerCase()) && 
          <>
         //show the data here  but nothing is showing here
          </>
      }
    }

  </>
 )

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多