这里我有两个数组,我需要将第一个数组转换为格式,如第二个数组...
第一个数组是google地图的行车路线,因此:response.routes[0].overview_path
会产生类似:
An array of LatLngs representing the entire course of this route. The path is simplified in order to make it suitable in contexts where a small number of vertices is required (such as Static Maps API URLs).
CODE
[
new google.maps.LatLng(12.34, 56.789),
new google.maps.LatLng(87.65, 123.45)
]
所以我有这个:
[
[56.789, 12.34],
[123.45, 87.65]
]
我需要使用以下格式的javascript对此数组进行转换:
[
[
{
X: 12.34
Y: 56.789
},
{
X: 87.65,
Y: 123.45
}
]
]
那我该怎么做呢? 有什么办法吗? 如何使用X和Y将第一个数组转换为第二个数组?
更新 我这样做:
tacke = response.routes[0].overview_path;
rezultat = [tacke.map(function(w) { return {X:w[1], Y:w[0]}; })];
现在console.log(rezultat);
生成此代码:
console.log(rezultat);
[Array[220]]
0: Array[220]
[0 … 99]
0: Object
X: undefined
Y: undefined
__proto__: Object
1: Object
X: undefined
Y: undefined
__proto__: Object
2: Object
X: undefined
Y: undefined
__proto__: Object
为什么这里有X和Y未定...
DEMO:http://jsbin.com/uTATePe/41代码:http://jsbin.com/uTATePe/41/edit
答案 0 :(得分:3)
var olddata = [
[56.789, 12.34],
[123.45, 87.65]
]
var newdata = [olddata.map(function(w) { return {X:w[1], Y:w[0]}; })];
答案 1 :(得分:2)