我有一个 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)
但是,我的第二个不起作用,因为拆分函数不会拆分数组类型。
如果需要,请尝试下一个{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));
});
您可以映射数组并将每个值拆分为多个值。
var array = [
'1,2',
'3,4',
];
var newArray = array.map(function(i) {
return i.split(',');
});
// Returns an array of arrays
// [ [1, 2], [3, 4] ]