1

How can we map nested objects in 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"
   }
}
}

This data is fetched from the server and I want to traverse through each element, and for that I want to map this data.

1

1 Answer 1

1

Use Object.keys() to map over all keys. Object.keys() reference

Object.keys(data).map(key => {
    const value = data[key];
    // do whatever you want with the data
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.