
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert List of String Coordinates to Float Lists of Latitude and Longitude 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 ]
Advertisements