1

I want to load one json file for a d3 world map (based on: http://datamaps.github.io/) and a d3 bar chart (based on: https://bl.ocks.org/mbostock/3885304).

My JSON looks like this:

{
"CHE":  {"costOfLivingIndex": "123.10", "propertyPriceToIncomeRatio": "123.10", "qualityOfLifeIndex": "208.54", "name": "Switzerland", "purchasingPower": "178.74", "pollutionIndex": "28.73", "trafficCommuteTimeIndex": "8.57", "health Care Index": "68.88", "climateIndex": "23.02", "safetyIndex": "74.27"},
"DNK":  {"costOfLivingIndex": "84.88", "propertyPriceToIncomeRatio": "84.88", "qualityOfLifeIndex": "206.49", "name": "Denmark", "purchasingPower": "142.14", "pollutionIndex": "25.64", "trafficCommuteTimeIndex": "5.85", "health Care Index": "81.89", "climateIndex": "29.93", "safetyIndex": "74.33"}
etc...
}

I choose this lay for the d3 world map. However, how I am wondering how I can iterate through properties of the objects. If I do for instance:

console.log(data.DNK.name);

I get the name, but how can get a list with all the names (or other values)?

CorneeldH
  • 593
  • 1
  • 8
  • 21

2 Answers2

0

we you could do something like:

for (var key in data) {
    alert(key + " -> " + data[key]);
}

you should be able to get the other parameters via data[key]

bwright
  • 896
  • 11
  • 29
0

As it is already JSON object you can loop through it without using JSON.parse which you would use if it were string or sth.

for(var key in data){
  console.log(key); 
    for(var k in data[key]){
      console.log(k,data[key][k]);
    }
}
avck
  • 3,535
  • 3
  • 26
  • 38