gpt4 book ai didi

javascript - 数组转换

转载 作者:行者123 更新时间:2023-11-29 17:14:10 26 4
gpt4 key购买 nike

这里我有两个数组,我需要将第一个数组转换为类似于第二个数组的格式...

第一个数组是来自谷歌地图的行车路线,所以: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).

代码

[
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未定义...

演示:http://jsbin.com/uTATePe/41代码:http://jsbin.com/uTATePe/41/edit

最佳答案

var olddata = [
[56.789, 12.34],
[123.45, 87.65]
]
var newdata = [olddata.map(function(w) { return {X:w[1], Y:w[0]}; })];

关于javascript - 数组转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com