亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

映射 csv 以在反應中顯示數組中的每個項目

映射 csv 以在反應中顯示數組中的每個項目

慕哥9229398 2022-01-20 17:13:27
我有一張產品表所以我在這里有一列product_images包含 id 數組(用作圖像源)。使用道具我正在映射這些產品,如下所示:render() {        return (            <>                <ul className="row">                    {this.props.products                        .map(product => <li className="card col-md-4" key={product.id}>                            <h2>{product.name}</h2>                            <p>{product.description}</p>                            <p>{product.sub_categories}</p>                            <b>{product.created}</b>                            {Object.keys(product.product_images)                                .filter(v => product.product_images[v] != null)                                .map(product_image =>                                    <div key={product.product_images}>                                        <img height='100%' alt='hello'                                         src={"IMAGE_PATH" + product.product_images[product_image]} />                                    </div>                                )}                        </li>)}                </ul>            </>        );    }這給了我每個產品。我還想得到的是數組中的每個 id 以在產品中顯示多個圖像。我知道我在映射對象時做錯了。作為 javascript 和 react 的初學者,如果你們能幫助我,那就太好了。我也對其他有趣的解決方案持開放態度。謝謝
查看完整描述

2 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

在我看來,您的 product_images 是產品圖像 ID 的 CSV。如果是這種情況,請嘗試:


{product.product_images.split(',')

  .filter(Boolean) // this is to filter out falsy values if necessary

  .map(product_image =>

      <div key={`${product.id}-${product_image}`}> // concat product id with the image id

          <img height='100%' alt='hello' src={`${IMAGE_PATH}${product_image}`} />

      </div>

  )}


查看完整回答
反對 回復 2022-01-20
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

Object.values((product && product.product_images) || [])

  .filter(productImgArr => productImgArr !== null)

  .map(productImgArr => productImgArr.join(''))

  .map((product_image, index) =>

      <div key={`${product_image}-${index}`}>

          <img height='100%' alt='hello' 

          src={"IMAGE_PATH" + product_image} />

      </div>

  )


查看完整回答
反對 回復 2022-01-20
  • 2 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號