Search code examples
javascriptarcgis

Convert object address to string


In my application when you single-click a point on the map it returns the address. Example:

locator.on("location-to-address-complete", function(evt) {
if (evt.address.address) {
      var address = evt.address.address;
      var location = webMercatorUtils.geographicToWebMercator(evt.address.location);
      console.log(address)
    }
});

console displays:

Object {Address: "1029 W Cermak Rd", Neighborhood: null, City: "Chicago", Subregion: null, Region: "Illinois"…}

Is there a way to convert the address to a string. Basically I just want: 1029 W Cermak Rd., Chicago, Illinois.

Thank you.


Solution

  • There is no generic way to do this. You just need to concatenate the fields you're interested in. You can use filter to filter out the nil/empty fields, and then join the results.

    var address = evt.address.address;
    
    addressString = [
      address.Address,
      address.Neighborhood,
      address.City,
      address.Region,
      address.Subregion,
    ].filter(function (val) { return val }).join(", ");