2017-06-24 39 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"] 
*/ 
相关问题