我从Firebase数据库获取数据,并希望能够将其传递到要在Google地图中使用的函数中。所需/期望的格式如下。我可以从Firebase获取数据,但是想以某种方式重新格式化以使其成为正确的格式。
我已经传递了数组,然后尝试将其推入位置数组,但这显然不起作用。
当前数组:
var array = ["Stop1, 37.3329891813207, -122.039420719004", "Stop2, 37.3320091546085, -122.036849794357", "Stop3, 37.3310547072562, -122.034668026436", "Stop4, 37.3301791417375, -122.033242200136", "Stop5, 37.3307185698498, -122.030606204886"]
initMap(array)
所需格式:
function initMap(passedArray) {
// ..在这里转换数组?..
var locations = [
['Stop1', 37.3329891813207, -122.039420719004],
['Stop2', 37.3320091546085, -122.036849794357],
['Stop3', 37.3310547072562, -122.034668026436],
['Stop4', 37.3301791417375, -122.033242200136],
['Stop5', 37.3307185698498, -122.030606204886]
];
}
感谢。
答案 0 :(得分:1)
按分隔符', '
拆分并解析数字。
var array = ["Stop1, 37.3329891813207, -122.039420719004", "Stop2, 37.3320091546085, -122.036849794357", "Stop3, 37.3310547072562, -122.034668026436", "Stop4, 37.3301791417375, -122.033242200136", "Stop5, 37.3307185698498, -122.030606204886"]
const initMap = array => array
.map(str => str.split(', ').map((x, i) => (i ? parseFloat(x) : x)))
console.log(initMap(array))