gpt4 book ai didi

Javascript 获取子数组的第二个元素(以逗号分隔)

转载 作者:行者123 更新时间:2023-12-03 00:35:00 27 4
gpt4 key购买 nike

我的 Controller 有一个输出数组,如下所示:

[
["48.85585695936588,2.317734961729684"],
["48.87234429654349,2.351466422300973"],
["48.85376742273335,2.3639977028185513"]
]

我想从这个坐标创建一个多边形,所以这个数据结构(数组):

array pos

这是我的代码:

for(var j = 0; j < output.length; j++) {
var points = [];
var quart = JSON.parse(output[j]['polygon']);
for (var i = 0; i < quart.length; i = i+2) {
points.push({
lat: parseFloat(quart[i]),
lng: parseFloat(quart[i+1])
});
}

我无法获取经度值(逗号后面的值)...

谢谢。

最佳答案

一种方法如下:

// original array:
let twoDimensionalArray = [
["48.85585695936588,2.317734961729684"],
["48.87234429654349,2.351466422300973"],
["48.85376742273335,2.3639977028185513"]
],
// using Array.prototype.map() to iterate over the
// Array, returning a new Array (assigned to the
// variable):
coordsArray = twoDimensionalArray.map(

// using an Arrow function to pass the current
// Array into the function:
(coords) => {

// here we use destructuring to assign
// values to a and b; to do this we take the last
// (and only) entry of of the coords Array, and
// split on the comma character. The first element
// of the Array created by String.prototype.split(),
// we then use Array.prototype.map() - along with
// another Arrow function to iterate over the returned
// Array to convert the Strings to numbers, with
// parseFloat():
[a, b] = coords.pop().split(',').map((xy)=>parseFloat(xy))

// here we return an Object with named 'lat' and 'lng'
// properties, assigning the relevant values to each:
return {
'lat': a,
'lng': b
}
});

console.log(coordsArray);

引用文献:

关于Javascript 获取子数组的第二个元素(以逗号分隔),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694448/

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