-3

How do I loop over the json array of objects having below json structure:

"ArrayOfAddress": [{
    "address": {
    "country": "US",
    "city":"NewYork"
    },
    "Type": "435473"
},{
    "address":{
        "country":"Germany",
        "city":"abc"
    },
    "Type":"124586"
}
]

and access country ,city and type values

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90

2 Answers2

0

I hope you're trying to access these properties after storing the json into an object.

a = {"ArrayOfAddress": [{
                "address": {
                    "country": "US",
                    "city":"NewYork"
                },
                "Type": "435473"
            },{
                                   "address":{
                                            "country":"Germany",
                                             "city":"abc"
                                         },
                                    "Type":"124586"

                              }

                 ]}
a.ArrayOfAddress.forEach((val) => {document.getElementById("content").innerHTML += (val.address.country + " " + val.address.city + " "+ val.Type) + "<br/>"})
<span id="content"></span>
shrys
  • 5,860
  • 2
  • 21
  • 36
0

What do you mean? Do you want the loop to go over every single leaf value? (country:us, city:NY, type:435473, country:germany, city:abc, type:123586)?