在 JS 中使用分隔符从一列数组创建两列数组

Create Two Column Array From One Column Array Using Delimiter in JS

我有一个 JavaScript 纬度和经度数组。现在,我的数组采用这种格式并且类型为 Array:

[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)

但是,我的第二个不起作用,因为拆分函数不拆分数组类型。

您可以通过数组映射并将每个值拆分为多个。

var array = [
  '1,2',
  '3,4',
];

var newArray = array.map(function(i) {
  return i.split(',');
});

// Returns an array of arrays
// [ [1, 2], [3, 4] ]

如果需要,请尝试下一个 {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));
});