0
[
 {
   "ISO 3166 Country Code": "AD",
   "Country": "Andorra",
   "Latitude": 42.5,
   "Longitude": 1.5
 },
 {
   "ISO 3166 Country Code": "AE",
   "Country": "United Arab Emirates",
   "Latitude": 24,
   "Longitude": 54
 },
 {
   "ISO 3166 Country Code": "AF",
   "Country": "Afghanistan",
   "Latitude": 33,
   "Longitude": 65
 },
 {
   "ISO 3166 Country Code": "AG",
   "Country": "Antigua and Barbuda",
   "Latitude": 17.05,
   "Longitude": -61.8
 }
]
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    There is only one JSON object. What you have is an object literal. What have you tried? Where did it go wrong? What output do you expect? – RobG Nov 07 '18 at 02:42
  • Possible duplicate of [How to convert JSON object to JavaScript array](https://stackoverflow.com/questions/14528385/how-to-convert-json-object-to-javascript-array) – tera_789 Nov 07 '18 at 02:45

4 Answers4

0

Do this:

var countries = [];

fetch("url.json").then(response => {
    return response.json();
}).then(data => {
    for (var i = 0; i < data.length; i++) {
        countries.push(data[i].Country);
    }
});
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

Use Array.map

let arr = [{"ISO 3166 Country Code":"AD","Country":"Andorra","Latitude":42.5,"Longitude":1.5},{"ISO 3166 Country Code":"AE","Country":"United Arab Emirates","Latitude":24,"Longitude":54},{"ISO 3166 Country Code":"AF","Country":"Afghanistan","Latitude":33,"Longitude":65},{"ISO 3166 Country Code":"AG","Country":"Antigua and Barbuda","Latitude":17.05,"Longitude":-61.8}];

let countries = arr.map(v => v.Country);
console.log(countries);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
0
var arr = [...]// posted in question
var countries = arr.map(function(element) { return element['Country'] })
Nick Ellis
  • 1,048
  • 11
  • 24
0

The easiest and most concise way to do it would be with array.map. However, if you consider performance to be an issue, using a for loop could be a magnitude of ten faster. See the following link for more information about performance https://hackernoon.com/javascript-performance-test-for-vs-for-each-vs-map-reduce-filter-find-32c1113f19d7

Donny Verduijn
  • 424
  • 6
  • 10