javascript - JS 使用分隔符从一列数组创建两列数组

标签 javascript jquery arrays latitude-longitude delimiter

我有一个包含纬度和经度的 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/

相关文章:

javascript - Jquery脚本删除表单集合中的所有项目

javascript - 如何将 uint8 数组转换为 base64 编码的字符串?

javascript - 如何从 firebase 逐个访问元素

javascript - Ckeditor5 - "widget toolbar no items"{toolbarId : 'mediaEmbed' }

javascript - 单击下拉菜单时不会转到链接

javascript - Kohana/JQuery Auth 访问问题

jquery - 打开文档时 Windows HTML5 应用程序出现 "DOCTYPE expected"错误

javascript - 在 Javascript 中查找数组最大值

javascript - 需要在选项卡菜单中向下滚动时隐藏背景图像

javascript - 分层数据,如何循环所有级别并将附加数据插入每个节点