简体   繁体   中英

Map inside filter reactjs

I try to map inside filter

This is my code

{Searchfile.filter(
                        (ids) =>
                          ids._id === user.favorite[0] ||
                          ids._id === user.favorite[1] ||
                          ids._id === user.favorite[2] ||
                          ids._id === user.favorite[3] ||
                          ids._id === user.favorite[4] ||
                          ids._id === user.favorite[5]

                        // user.favorite.map((item, index) => ids._id === item)
                      ).map((item, index) => (
                        <div
                          className="row-queue"
                          key={index}
                          // onClick={() => handleClick(item, index)}
                        >
                          <div className="column1-queue">{index + 1}</div>
                          <div className="column2-queue">{item.title}</div>
                          <div className="column3-queue">{item.singer}</div>
                        </div>
                      ))}

Can I Map user.favorite, so I don't need to add manually?

You can check if the user.favorite array includes the ids._id value.

{Searchfile.filter((ids) => user.favorite.includes(ids._id)).map(
  (item, index) => (
    <div
      className="row-queue"
      key={index}
      // onClick={() => handleClick(item, index)}
    >
      <div className="column1-queue">{index + 1}</div>
      <div className="column2-queue">{item.title}</div>
      <div className="column3-queue">{item.singer}</div>
    </div>
  )
)}

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