-3

I have this JSON string assigned to a variable.

<script>var Countries = {"Countries": [
{"locname":"Argentina","locCountry":"AR","ProductCount":"8","BreweryCount":"2"},
{"locname":"Austria","locCountry":"AT","ProductCount":"5","BreweryCount":"2"},
{"locname":"Belgium","locCountry":"BE","ProductCount":"19","BreweryCount":"4"},
{"locname":"Brazil","locCountry":"BR","ProductCount":"23","BreweryCount":"3"},
{"locname":"Canada","locCountry":"CA","ProductCount":"36","BreweryCount":"7"},
{"locname":"Chile","locCountry":"CL","ProductCount":"1","BreweryCount":"1"},
{"locname":"China","locCountry":"CN","ProductCount":"97","BreweryCount":"6"},
{"locname":"Dominica","locCountry":"DM","ProductCount":"1","BreweryCount":"1"},
{"locname":"France","locCountry":"FR","ProductCount":"42","BreweryCount":"2"},
{"locname":"Germany","locCountry":"DE","ProductCount":"26","BreweryCount":"3"},
{"locname":"Guatemala","locCountry":"GT","ProductCount":"1","BreweryCount":"1"},
{"locname":"Israel","locCountry":"IL","ProductCount":"1","BreweryCount":"1"},
{"locname":"Italy","locCountry":"IT","ProductCount":"5","BreweryCount":"1"},
{"locname":"Japan","locCountry":"JP","ProductCount":"47","BreweryCount":"1"},
{"locname":"South Korea","locCountry":"KR","ProductCount":"6","BreweryCount":"2"},
{"locname":"Lithuania","locCountry":"LT","ProductCount":"3","BreweryCount":"1"},
{"locname":"Mexico","locCountry":"MX","ProductCount":"24","BreweryCount":"4"},
{"locname":"Netherlands","locCountry":"NL","ProductCount":"21","BreweryCount":"3"},
{"locname":"Romania","locCountry":"RO","ProductCount":"1","BreweryCount":"1"},
{"locname":"Russia","locCountry":"RU","ProductCount":"1","BreweryCount":"1"},
{"locname":"South Africa","locCountry":"ZA","ProductCount":"5","BreweryCount":"2"},
{"locname":"Spain","locCountry":"ES","ProductCount":"79","BreweryCount":"7"},
{"locname":"Turkey","locCountry":"TR","ProductCount":"8","BreweryCount":"1"},
{"locname":"United Arab Emirates","locCountry":"AE","ProductCount":"1","BreweryCount":"1"},
{"locname":"United States","locCountry":"US","ProductCount":"687","BreweryCount":"33"},
{"locname":"Viet Nam","locCountry":"VN","ProductCount":"5","BreweryCount":"2"},
{"locname":"United Kingdom","locCountry":"GB","ProductCount":"8","BreweryCount":"3"}]}

How do I loop thru this to get the different values? I have tried Countries[1].locname, but that gets me can't read property name error. What am I doing wrong?

  • 1
    Possible duplicate of [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – choz May 22 '19 at 16:14
  • Use `Countries.Countries.forEach(x => console.log(x))` – Maheer Ali May 22 '19 at 16:15
  • Use [for...in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) for the object - then [for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) for the array – choz May 22 '19 at 16:15
  • It looks like a JSON, not a JSON string. – goodvibration May 22 '19 at 16:17
  • 1
    That's an object, not JSON. [You are dealing with JavaScript, not JSON](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Quentin May 22 '19 at 16:18
  • @goodvibration There is no such thing as "a JSON"; as Quentin notes, they are called objects, not "JSONs". – Heretic Monkey May 22 '19 at 16:19
  • @HereticMonkey: I know, and was actually thinking to add it in parenthesis. This dude wrote "JSON string", I wanted to emphasize that it was not a string, hence phrased this sentence the way I did. – goodvibration May 22 '19 at 16:21
  • Thanks for the comments. The vocabulary is something I am still figuring out – Jeff Hurley May 22 '19 at 16:23

1 Answers1

-2

This should provide insight

var Countries = {
  "Countries": [{
      "locname": "Argentina",
      "locCountry": "AR",
      "ProductCount": "8",
      "BreweryCount": "2"
    },
    {
      "locname": "Austria",
      "locCountry": "AT",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "Belgium",
      "locCountry": "BE",
      "ProductCount": "19",
      "BreweryCount": "4"
    },
    {
      "locname": "Brazil",
      "locCountry": "BR",
      "ProductCount": "23",
      "BreweryCount": "3"
    },
    {
      "locname": "Canada",
      "locCountry": "CA",
      "ProductCount": "36",
      "BreweryCount": "7"
    },
    {
      "locname": "Chile",
      "locCountry": "CL",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "China",
      "locCountry": "CN",
      "ProductCount": "97",
      "BreweryCount": "6"
    },
    {
      "locname": "Dominica",
      "locCountry": "DM",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "France",
      "locCountry": "FR",
      "ProductCount": "42",
      "BreweryCount": "2"
    },
    {
      "locname": "Germany",
      "locCountry": "DE",
      "ProductCount": "26",
      "BreweryCount": "3"
    },
    {
      "locname": "Guatemala",
      "locCountry": "GT",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Israel",
      "locCountry": "IL",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Italy",
      "locCountry": "IT",
      "ProductCount": "5",
      "BreweryCount": "1"
    },
    {
      "locname": "Japan",
      "locCountry": "JP",
      "ProductCount": "47",
      "BreweryCount": "1"
    },
    {
      "locname": "South Korea",
      "locCountry": "KR",
      "ProductCount": "6",
      "BreweryCount": "2"
    },
    {
      "locname": "Lithuania",
      "locCountry": "LT",
      "ProductCount": "3",
      "BreweryCount": "1"
    },
    {
      "locname": "Mexico",
      "locCountry": "MX",
      "ProductCount": "24",
      "BreweryCount": "4"
    },
    {
      "locname": "Netherlands",
      "locCountry": "NL",
      "ProductCount": "21",
      "BreweryCount": "3"
    },
    {
      "locname": "Romania",
      "locCountry": "RO",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "Russia",
      "locCountry": "RU",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "South Africa",
      "locCountry": "ZA",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "Spain",
      "locCountry": "ES",
      "ProductCount": "79",
      "BreweryCount": "7"
    },
    {
      "locname": "Turkey",
      "locCountry": "TR",
      "ProductCount": "8",
      "BreweryCount": "1"
    },
    {
      "locname": "United Arab Emirates",
      "locCountry": "AE",
      "ProductCount": "1",
      "BreweryCount": "1"
    },
    {
      "locname": "United States",
      "locCountry": "US",
      "ProductCount": "687",
      "BreweryCount": "33"
    },
    {
      "locname": "Viet Nam",
      "locCountry": "VN",
      "ProductCount": "5",
      "BreweryCount": "2"
    },
    {
      "locname": "United Kingdom",
      "locCountry": "GB",
      "ProductCount": "8",
      "BreweryCount": "3"
    }
  ]
};
console.log(Countries.Countries);
console.log(Countries.Countries[1]);
console.log(Countries.Countries[1].locname);
for (let i = 0; i < Countries.Countries.length; i++) {
  console.log(Countries.Countries[i]);
}
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100