Javascript 将二维阵列转换为多线对象
我想把一个丑陋的数组转换成一个多行的酷对象,但我的大脑让我失望了。 我想把这个丑陋的东西Javascript 将二维阵列转换为多线对象,javascript,reactjs,Javascript,Reactjs,我想把一个丑陋的数组转换成一个多行的酷对象,但我的大脑让我失望了。 我想把这个丑陋的东西 "location" : { "type" : "Polygon", "coordinates" : [ [ [ -8.61901849508286, 41.5311338460033 ],
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
-8.61901849508286,
41.5311338460033
],
[
-8.61851692199707,
41.5311338460033
],
[
-8.61851692199707,
41.5312944769825
],
[
-8.61901849508286,
41.5312944769825
],
[
-8.61901849508286,
41.5311338460033
]
]
]
},
对于这件神奇的事情:
const paths = [
{ lat: 41.53113384600326, lng: -8.619018495082855 },
{ lat: 41.53113384600326, lng: -8.61851692199707 },
{ lat: 41.53129447698251, lng: -8.61851692199707 },
{ lat: 41.53129447698251, lng: -8.619018495082855 },
{ lat: 41.53113384600326, lng: -8.619018495082855 }
];
我试过这个
const polygon = this.props.places.places.map(place => {
return place.location.coordinates.map(([lat, lng]) => ({ lat, lng }));
});
我试过了,但是我失败了,失败了。有人知道怎么做吗?你可以这样做:
place.location.coordinates.map(
(values) => values.map(
(data) => {lat: data[0], lng: data[1]}
)
);
试试这个解决方案
//wrap the "ugly object" in curly brackets so it becomes a javascript object
var locationCoordinates =
{
"location" : {
"type" : "Polygon",
"coordinates" :
[
[
[
-8.61901849508286,
41.5311338460033
],
[
-8.61851692199707,
41.5311338460033
],
[
-8.61851692199707,
41.5312944769825
],
[
-8.61901849508286,
41.5312944769825
],
[
-8.61901849508286,
41.5311338460033
]
]
]
}
};
//get the coordinates portion of the object
var coordinates = locationCoordinates.location.coordinates[0];
var paths = [];//the amazing thing
coordinates.forEach(function(value, index, array)
{ //generate the required format and add it to the paths variable
paths.push({lat: value[1], lng: value[0]});
});
console.log(paths); //displays the required output
我希望这有帮助 不客气,请考虑验证响应以关闭该主题:)