I'm trying to get latitude and longitude value out of string. If I set the location on the map, the map api gives me string value like below.

var location = {map: geo="20.471884,-157.505,6" p="Hawaii"}

and I'd like to get the number values 20.471884, -157.505 and 6 into var lat, lng.

how can I make it in javascript??

1 Answers

Jack Bashford On Best Solutions

Firstly, make your object valid. Then just split on a comma to extract the desired values.

let obj = {
  map: {
    geo: "20.471884,-157.5056",
    p: "Hawaii"

const [lat, lng] = obj.map.geo.split(",");

The name is obj not location because location is reserved.