2017-06-24 71 views
-2

我在地圖上繪製道和步道的座標保存爲字符串在以下格式的JSON文件: (43.886758784865066,24.226741790771484),(43.90271630763887,24.234981536865234)如何將一串地圖座標轉換爲數組javascript?

我需要得到這些值並將它們添加到數組中: coordinates = [43.886758784865066,24.226741790771484,43.90271630763887,24.234981536865234];

那麼我該如何做這個過渡?

回答

0

你可以嘗試這樣

var string = '(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)'; 
string.match(/\d+(\.\d+)/g).map(function(d){return d;}); 
0

您可以使用正則表達式來解析這些字符串。

const match = string.match(/\((.*)\, (.*)\),\((.*)\, (.*)\)/) 
/* 
    Matches 
    ["(43.886758784865066, 24.226741790771484),(43.90271630763887, 24.234981536865234)", "43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"] 
*/ 
const Array.prototype.slice.call(match).splice(1, 4) 
/* Converts to array and takes the last three elements 
["43.886758784865066", "24.226741790771484", "43.90271630763887", "24.234981536865234"] 
*/ 
相關問題