Convert a list of string coords into two float lists of Lat/Longitude coordinates in JavaScript?

When working with coordinate data in JavaScript, you often need to parse string coordinates and separate them into latitude and longitude arrays. This is common when processing GPS data or API responses.

Input Data Format

Let's start with a list of coordinate strings in "latitude,longitude" format:

var listOfStrings = ["10.45322,-6.8766363", "78.93664664,-9.74646646", "7888.7664664,-10.64664632"];
console.log("Input coordinates:");
console.log(listOfStrings);
Input coordinates:
[ '10.45322,-6.8766363', '78.93664664,-9.74646646', '7888.7664664,-10.64664632' ]

Method 1: Using forEach with split() and map()

This approach uses split() to separate coordinates and map(Number) to convert strings to floats:

var listOfStrings = ["10.45322,-6.8766363", "78.93664664,-9.74646646", "7888.7664664,-10.64664632"];
var latitude = [];
var longitude = [];

listOfStrings.forEach(coords => coords.split(',')
    .map(Number)
    .forEach((value, index) => [latitude, longitude][index].push(value))
);

console.log("Latitude values:");
console.log(latitude);
console.log("Longitude values:");
console.log(longitude);
Latitude values:
[ 10.45322, 78.93664664, 7888.7664664 ]
Longitude values:
[ -6.8766363, -9.74646646, -10.64664632 ]

Method 2: Using map() for Cleaner Code

A more readable approach using map() to extract coordinates:

var listOfStrings = ["10.45322,-6.8766363", "78.93664664,-9.74646646", "7888.7664664,-10.64664632"];

var latitude = listOfStrings.map(coords => parseFloat(coords.split(',')[0]));
var longitude = listOfStrings.map(coords => parseFloat(coords.split(',')[1]));

console.log("Latitude array:");
console.log(latitude);
console.log("Longitude array:");
console.log(longitude);
Latitude array:
[ 10.45322, 78.93664664, 7888.7664664 ]
Longitude array:
[ -6.8766363, -9.74646646, -10.64664632 ]

Method 3: Using reduce() for Single Pass

Process all coordinates in a single pass using reduce():

var listOfStrings = ["10.45322,-6.8766363", "78.93664664,-9.74646646", "7888.7664664,-10.64664632"];

var result = listOfStrings.reduce((acc, coords) => {
    var [lat, lng] = coords.split(',').map(Number);
    acc.latitude.push(lat);
    acc.longitude.push(lng);
    return acc;
}, { latitude: [], longitude: [] });

console.log("Separated coordinates:");
console.log("Latitudes:", result.latitude);
console.log("Longitudes:", result.longitude);
Separated coordinates:
Latitudes: [ 10.45322, 78.93664664, 7888.7664664 ]
Longitudes: [ -6.8766363, -9.74646646, -10.64664632 ]

Comparison

Method Readability Performance Use Case
forEach with split Medium Good When modifying existing arrays
map() approach High Good Clean, functional style
reduce() approach High Best Single pass processing

Conclusion

All three methods effectively convert coordinate strings into separate latitude and longitude arrays. The map() approach offers the best readability, while reduce() provides optimal performance for large datasets.

Updated on: 2026-03-15T23:18:59+05:30

656 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements