2

I'm looking for a way to search text "country" in below object.

{
  "plus_code": {
    "compound_code": "F28M+H8 Gurugram, Haryana, India",
    "global_code": "7JWVF28M+H8"
  },
  "results": [{
    "address_components": [{
      "long_name": "53/1",
      "short_name": "53/1",
      "types": ["premise"]
    }, {
      "long_name": "Block B",
      "short_name": "Block B",
      "types": ["political", "sublocality", "sublocality_level_3"]
    }, {
      "long_name": "Acharya Puri",
      "short_name": "Acharya Puri",
      "types": ["political", "sublocality", "sublocality_level_2"]
    }, {
      "long_name": "Sector 12",
      "short_name": "Sector 12",
      "types": ["political", "sublocality", "sublocality_level_1"]
    }, {
      "long_name": "Gurugram",
      "short_name": "Gurugram",
      "types": ["locality", "political"]
    }, {
      "long_name": "Gurgaon",
      "short_name": "Gurgaon",
      "types": ["administrative_area_level_2", "political"]
    }, {
      "long_name": "Haryana",
      "short_name": "HR",
      "types": ["administrative_area_level_1", "political"]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": ["country", "political"]
    }, {
      "long_name": "122001",
      "short_name": "122001",
      "types": ["postal_code"]
    }],
    "formatted_address": "53/1, Block B, Acharya Puri, Sector 12, Gurugram, Haryana 122001, India",
    "geometry": {
      "location": {
        "lat": 28.4662822,
        "lng": 77.03326179999999
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "northeast": {
          "lat": 28.4676311802915,
          "lng": 77.0346107802915
        },
        "southwest": {
          "lat": 28.4649332197085,
          "lng": 77.0319128197085
        }
      }
    },
    "place_id": "ChIJ2Up-YTUYDTkR3vg0BXrwyXI",
    "plus_code": {
      "compound_code": "F28M+G8 Gurugram, Haryana, India",
      "global_code": "7JWVF28M+G8"
    },
    "types": ["street_address"]
  }],
  "status": "OK"
}

In the above object which has types": ["country","political" ] I want its parent variable short_name value in variable.

Right now I'm using the following code:

if (locationData.status == "OK") {
  var data = locationData.results;
  var dataLength = data.length;
  if (data[dataLength - 1].hasOwnProperty("address_components")) {
    var address_components = data[dataLength - 1].address_components;
    var countryCode = address_components[0].short_name.toLocaleLowerCase();
    if (countryCode && pageData.hasOwnProperty(countryCode)) {
      localStorage.setItem("el_country", countryCode);
      location.href = pageData[countryCode];
    } else {
      location.href = defaultPage;
    }
  }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Rohit
  • 23
  • 5

1 Answers1

1

You can use filter() to find entities within the results[].address_components array which contain the country string within their types. Try this:

// Note: I removed a lot of the unnecesary properties from the object 
// for this demo for the sake of brevity

let locationData = {
  "results": [{
    "address_components": [{
      "long_name": "Haryana",
      "short_name": "HR",
      "types": ["administrative_area_level_1", "political"]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": ["country", "political"]
    }, {
      "long_name": "122001",
      "short_name": "122001",
      "types": ["postal_code"]
    }]
  }],
  "status": "OK"
}

// IE-friendly version:
// let countries = locationData.results[0].address_components.filter(function(o) {    
//   return o.types.includes('country');
// });

let countries = locationData.results[0].address_components.filter(o => o.types.includes('country'));
let shortName = countries[0].short_name;

console.log(shortName);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • How to support below function for IE11, where filter, arrow and include function is not supported? "let countries = locationData.results[0].address_components.filter(o => o.types.includes('country'));" – Rohit Jan 22 '21 at 07:23
  • You can revert to an anonymous function for IE support. I updated the answer to show you how to do this – Rory McCrossan Jan 22 '21 at 10:32
  • I am getting console error in IE11 "Object doesn't support property or method 'includes'" for following code: return o.types.includes('country'); – Rohit Jan 25 '21 at 17:18
  • IE doesn't support `includes()`. You can use `indexOf()` instead: https://stackoverflow.com/a/36574502/519413 – Rory McCrossan Jan 25 '21 at 17:29