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



Let’s say the following are our coordinates −

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

To convert the above into two float lists of Latitude and Longitude, use split() on the basis of comma(,) along with map().

Example

var listOfStrings = ["10.45322,-6.8766363", "78.93664664,-9.74646646", "7888.7664664,-10.64664632"]; var latitude = []; var longitude = []; listOfStrings.forEach(obj => obj.split(',') .map(Number) .forEach((value, index) => [latitude, longitude][index].push(value)) ); console.log("All positive value is latitude=") console.log(latitude); console.log("All negative value is longitude=") console.log(longitude);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo180.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo180.js All positive value is latitude= [ 10.45322, 78.93664664, 7888.7664664 ] All negative value is longitude= [ -6.8766363, -9.74646646, -10.64664632 ]
Updated on: 2020-09-12T09:00:14+05:30

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements