如何对Object Reactjs中的数组进行迭代

t0ybt7op  于 2023-03-08  发布在  React
关注(0)|答案(2)|浏览(141)

问题:**我想访问图片数组每个图片为单一产品。**我想访问图片为单一产品。这是我自己的本地API文件,我在reactjs我正试图使电子商务商店的投资组合

{
        id:11,
        name:"Fresco Dining Set",
        price: 178000,
        status:"In Stock",
        totalquantity: 1,
        totalprice: 178000,
        description:"Contemporary design Gold-sharp black legs which complement Enza's custom design",
        category:{
            id:2,
            name:"Chair"
        },
        images:[
            "images/chairs/fresco_dining_set.jpg",
            "images/chairs/fresco_chair.jpg",
            "images/chairs/fresco_dining_table.png",
        ],
        rating:4.0,
        createdate:"2023-03-28"
    },

问题:我想访问单个产品的每个图像数组。

images:[
            "images/chairs/fresco_dining_set.jpg",
            "images/chairs/fresco_chair.jpg",
            "images/chairs/fresco_dining_table.png",
        ],

这是我的代码的权利。我试图使单一的产品细节。

const SingelProduct = () => {
  const dispatch=useDispatch();
  const {singleData}=useSelector(state=>state.api);
    const param=useParams();
    let pageid=Number(Object.values(param));
    useEffect(()=>{
        dispatch(fetchData(data)) 
        
      dispatch(setDetails(pageid))
  },[data])
    
 
  
  return (
    <>
        <SingleProductWrapper>
        <div className="singleproduct">
                <div className="singlecontainer">
                         <div className="slideimg">
                          <img src={`/${singleData.images}`}/>
                         </div>
                      
                    
                    <div className="singleproductdetail">
                       <h1 className="singlename singlecommon">{singleData.name}</h1>
                       <p className="singlerating singlecommon">Rating: <span>{singleData.rating}</span></p>
                       
                       <h1 className="singleprice singlecommon">Rs <span>{singleData.price}</span></h1>
                       <p className="singledescription singlecommon">
                        {singleData.description}
                       </p>
                       <div className="addtocart">
                         <div className="counter">
                          <button className="dec" onClick={()=>dispatch(minus())}>
                            <AiOutlineMinus/>
                          </button>
                          <div className="counter-input">{singleData.totalquantity}</div>
                          <button className="inc" onClick={()=>dispatch(add())}>
                            <AiOutlinePlus/>
                          </button>
                         </div>

                         <div className="addtocart-button">
                          <button type="button"   onClick={()=>dispatch(addtocart(singleData))}>Add to Cart</button>
                         </div>
                       </div>
                    </div>
                </div>
            </div>
        
           
        </SingleProductWrapper>     
    </>
  )
}

export default SingelProduct```

Is it good practice to place array in objects like this?
cyej8jka

cyej8jka1#

由于您有一个图像数组,因此需要返回几个图像,例如:

<div className="slideimg">
    {singleData?.images?.map((imgSrc) => <img key={imgSrc} src={`/${imgSrc}`}/>)}
</div>
j9per5c4

j9per5c42#

感谢兄弟们!我在这里张贴答案,所以如果其他人可以得到帮助。

{singleData.images?singleData.images.map(cur=>{
     return (<img key={cur} src={`/cur`}/>)
 }):"null"}

相关问题