Javascript 在JS中使用分隔符从一个列数组创建两个列数组
我有一个JavaScript的纬度和经度数组。现在,我的数组采用以下格式,类型为array:Javascript 在JS中使用分隔符从一个列数组创建两个列数组,javascript,jquery,arrays,latitude-longitude,delimiter,Javascript,Jquery,Arrays,Latitude Longitude,Delimiter,我有一个JavaScript的纬度和经度数组。现在,我的数组采用以下格式,类型为array: [Lat,Lon] [Lat,Lon] [Lat,Lon] 我想将此单列数组转换为具有以下格式的两列数组: [Lat][Lon] [Lat][Lon] [Lat][Lon] 如何在JS中执行此操作?我的最佳猜测是使用单列数组中的逗号作为分隔符,但我不确定如何实现这一点。我愿意使用JQuery 我试图使用此代码分割数据,但 var getOutline = 'lat1,lon1/lat2,lo
[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函数不分割数组类型。您可以映射数组并将每个值分割为多个值
var array = [
'1,2',
'3,4',
];
var newArray = array.map(function(i) {
return i.split(',');
});
// Returns an array of arrays
// [ [1, 2], [3, 4] ]
可以映射数组并将每个值拆分为多个值
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));
});
如果需要,请尝试下一个
{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));
});
请向我们展示您自己创建此变量的尝试_arr=[];arr.map(function(item){u arr.push(item.split(',')})@CarstenLøvboAndersen editeddefinite为此使用jQuery,它几乎可以做任何事情。请向我们展示您自己创建此变量的尝试_arr=[];arr.map(function(item){u arr.push(item.split(',')})@CarstenLøvboAndersen editeddefinite使用jQuery,它可以做任何事情。不确定这正是op想要的是我也不完全确定,希望他能澄清。不确定这正是op想要的是我也不完全确定,希望他能澄清。谢谢!你的第二个解决方案正是我所需要的。我想我误解了这个问题,因为这正是你说你不想要的格式:
[“lat1”、“lon1”]、[“lat2”、“lon2”]、[“lat3”、“lon3”]
@AlexanderHiggins,我更新了回复以将字符串转换为数字,而不是字符串或数字。问题说明数据已经是[Lat,Lon]
格式,但它是[Lat],[Lon]
格式所必需的,但随后接受的答案是[Lat,Lon]
格式。只是OP的拆分功能有问题。假设新读者可以使用此答案将[[Lat,Lon],[Lat,Lon]
格式的数据转换为二维数组,这将误导他们。谢谢!您的第二个解决方案正是我所需要的。我猜我误解了这个问题,因为这正是您所说的您不想要的格式:[“lat1”,“lon1”],[“lat2”,“lon2”],[“lat3”,“lon3”]
@AlexanderHiggins,我已经更新了回复,将字符串转换为数字,这与字符串或数字无关。问题说明数据已经在[Lat,Lon]
格式中,但它是[Lat],[Lon]
格式中需要的,但接受的答案最终是[Lat,Lon]
格式。只是OP的拆分功能有问题。如果新读者认为他们可以使用此答案将[[Lat,Lon],[Lat,Lon]
格式的数据转换为二维数组,那么这将误导他们。