我有一个包含纬度和经度的 JavaScript 数组。现在,我的数组采用这种格式并且属于数组类型:
[Lat,Lon]
[Lat,Lon]
[Lat,Lon]
我想将这个一列数组转换为具有以下格式的两列数组:
[Lat][Lon]
[Lat][Lon]
[Lat][Lon]
如何在 JS 中做到这一点?我最好的猜测是使用一列数组中的逗号作为分隔符,但我不确定如何实现这一点。我愿意使用 JQuery。
我尝试使用此代码分割我的数据,但是
var getOutline = 'lat1,lon1/lat2,lon2/lat3,lon3'; //Sample
var temporaryArray = new Array();
temporaryArray = getOutline.split("/");
console.log(temporaryArray)
var temporaryArray2 = new Array();
temporaryArray2 = temp.split(",");
console.log(temporaryArray2)
但是,我的第二个不起作用,因为 split 函数不会拆分数组类型。
最佳答案
如果需要,请尝试下一个{lat1: {lon1: value, lon2: ...}, ...}
:
var getOutline = 'lat1,lon1/lat2,lon2/lat3,lon3',
result = {};
getOutline.split('/').forEach(function (coord) {
var tmp = coord.split(',');
result[tmp[0]][tmp[1]] = '{something that is needed as a value}';
});
或者,如果需要[[lat1, lon1], [lat2, lon2], ...]
:
var getOutline = 'lat1,lon1/lat2,lon2/lat3,lon3',
result = [];
getOutline.split('/').forEach(function (coord) {
result.push(coord.split(',').map(Number));
});
关于javascript - JS 使用分隔符从一列数组创建两列数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230503/