0

I am currently implementing google places autocomplete and the module I am using in React Native gives me the address as a whole string and not in address components. However, I need to have the postal code and city separate. The example response always look like this:

address: 'Calle Gran Vía, 8, 28013 Madrid, Spain

From this string I would need to have an object that looks like this:

{
  city: 'Madrid',
  postal_code: 28013,
}

How could I achieve this?

Walter Monecke
  • 2,386
  • 1
  • 19
  • 52
  • Split at comma first, and then split the relevant part again at the space character …? – CBroe May 24 '18 at 12:30
  • Is the data always in this format "Calle Gran Vía, 8, 28013 Madrid, Spain" or it can vary? – Superluminal May 24 '18 at 12:32
  • @sgerodes the format is consistent – Walter Monecke May 24 '18 at 12:32
  • @Walter Monecke If the value is constant you can try this: var addrSplit = address.split(',')[2].trim().split(' '); var objectResult = { city: addrSplit[1], postal_code: addrSplit[0]}; – Nandha MV May 24 '18 at 12:45
  • Almost every answer addresses how to get this information from the string. The problem is that by doing this you are inviting bugs to your system when the string contains an unexpected comma or space. You should probably investigate if you can get the information in another format from google's API. – Rob Monhemius May 24 '18 at 12:57

6 Answers6

1

It's not the most "clean" or "smooth" answer, but it's something:

var response = "address: 'Calle Gran Vía, 8, 28013 Madrid, Spain";

var subStr = response.split(",")[2];

var obj = {
  city: subStr.split(" ")[2],
  postal_code: subStr.split(" ")[1]
};

console.log(obj);
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
1

For the city I think the best way is to use an array of cities and search it in the string

var str = "Calle Gran Vía, 8, 28013 Madrid, Spain";
var cities = ["Paris", "Berlin", "Madrid"];
var city = cities.filter(function(item) {
  if (str.search(item) != -1)
    return item;
})[0] || null;

For the postal code you should use a regex depending on the country (a good list of regex by country)

sanjar
  • 1,081
  • 2
  • 9
  • 15
0

Probably split the string by ',' with array methods, take the third element of the array and split that by ' ', then you have your data points.

Bezlonir
  • 66
  • 5
0

If you can always count on it being in that same format, you can do the following.

var splitAdress = address.split(",");
//This will give you ["Calle Gran Vía", " 8", " 28013 Madrid", " Spain"]
splitAdress = splitAdress[2].split(" ");
//This will give you ["", "28013", "Madrid"]

You'll first split the string into an array based on the comma and then follow it up by splitting on the space. The extra element in the second array is due to the space. This is an example of what @CBroe pointed out in the comments.

Foxhound013
  • 301
  • 3
  • 13
-1
list=adress.split(",")[2].split()

list[0] gives you the postal code

list[1] gives you the city name

It depend on if there is always a comma in the "Calle Gran Vía, 8", if not you can use instead list=adress.split(",")[-2].split()

Superluminal
  • 947
  • 10
  • 23
-1

You might want to try this.

var address="Calle Gran Vía, 8, 28013 Madrid, Spain";
var splits = address.split(',')[2].trim().split(' ');
var newAdd = {
    city : splits[1],
    postal_code : splits[0]
}
console.log(newAdd);
MJN
  • 610
  • 1
  • 7
  • 19