【问题标题】:How to map this using react js?如何使用 react js 映射它?
【发布时间】:2021-08-08 01:16:02
【问题描述】:

我们如何在 ReactJS 中映射嵌套对象?

{
   "Data1":{
      "attribute1":{
         "key":"discount",
         "value":"50% off"
      },
      "attribute2":{
         "key":"original_price",
         "value":"Rs. 998"
      },
      "attribute3":{
         "key":"img",
         "value":[
            ""
         ]
      },
      "attribute4":{
         "key":"product_url",
         "value":"https://www.flipkart.com/3six5-combo-pack-2-men-sports-running-shoes-walking/p/itm45eecec10cd8d"
      },
      "attribute5":{
         "key":"title",
         "value":"3SIX5 - Combo pack of 2 Men Sports & Running Sports Shoes Walking Shoes For Men"
      },
      "attribute6":{
         "key":"price",
         "value":"Rs. 498"
      },
      "buttonReference":"Button1"
   }
}
}

这些数据是从服务器获取的,我想遍历每个元素,为此我想映射这些数据。

【问题讨论】:

标签: json reactjs mapping


【解决方案1】:

使用Object.keys() 映射所有键。 Object.keys() reference

Object.keys(data).map(key => {
    const value = data[key];
    // do whatever you want with the data
});

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 2022-08-04
    • 1970-01-01
    • 2023-01-07
    • 2022-12-29
    • 2019-01-29
    • 2023-01-15
    • 2020-11-22
    • 2020-09-14
    相关资源
    最近更新 更多