簡體   English   中英

映射道具時在反應中丟失鍵值對

[英]Losing key value pair in react when mapping over props

在向 API 發出多個請求后,我promise.all()請求並發出另一個請求,保留上次請求中的一些屬性。 這給了我:

(4) [{…}, {…}, {…}, {…}]
0: {product_id: "2", results: Array(4), productInfo: {…}}
1: {product_id: "3", results: Array(6), productInfo: {…}}
2: {product_id: "8", results: Array(9), productInfo: {…}}
3: {product_id: "7", results: Array(5), productInfo: {…}}
length: 4
__proto__: Array(0)

productInfo: {...}是我想要的鍵值對。 在底部的這個組件中,我console.log()對象出現了,但是它在map()丟失了? 為什么會這樣,我該如何解決?

const FormatRelated = ({ recommended }) => {
  //  eslint-disable-next-line no-console
  console.log(recommended)
  return (
    <div className="recommendedThumbNailsRow">
      {recommended.map(obj => {
        //  eslint-disable-next-line no-console
        console.log(obj);
        return <DisplayRelatedImg styles={obj} />;
      })}
    </div>
  );
};

Map 函數是一個 Array 函數Array Map

如果你想遍歷對象道具,你應該使用這樣的東西:

Object.keys(recommended).map(key => console.log(recommended[key]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM