在 JavaScript 中將字串座標列表轉換為經緯度座標的兩個浮點數列表?


假設以下為我們的座標 −

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

要將上述座標轉換為經緯度的兩個浮點數列表,請使用 split() based on comma(,) along with map()。

示例

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);

要執行上述程式,你需要使用以下命令 −

node fileName.js.

此處,我的檔名是 demo180.js。

輸出

這將產生以下輸出 −

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 ]

更新於: 12-Sep-2020

534 次瀏覽

開啟您的職業生涯

完成課程認證

開始學習
廣告