我正在使用leafletjs在openstretmap上显示一些多边形。
我有一个外部数据资源,它为我提供了多边形的坐标。不幸的是,这个数组的坐标顺序错误。
实施例: 我明白了:
[[10.5254913,52.2734311],[10.5258872,52.2734632]]
我需要:
[[52.2734311,10.5254913],[52.2734632,10.5258872]]
所以我给自己写了一个小函数,它遍历数组并反转条目:
var polCoords = [];
$.each(value.polygon[0], function(key,value){
polCoords[key] = [value[1],value[0]];
});
这很好用。但现在我发现多边形的一些数组是多维的!所以我有一个像这样的数组:
[[[10.5261828,52.2726556],[10.5263222,52.2726767],[10.5263578,52.2726821],[10.5263637,52.2726677],[10.5263738,52.2726428],[10.5264042,52.2725678],[10.526186,52.2725346],[10.5261395,52.272649],[10.5261828,52.2726556]],[[10.5261828,52.2726556],[10.5261713,52.2726821],[10.5261621,52.2727047],[10.5259248,52.2726687],[10.5257879,52.2726479],[10.5257435,52.2727573],[10.5258014,52.2727661],[10.5257967,52.2727777],[10.5260173,52.2728113],[10.5261107,52.2728254],[10.5260641,52.2729403],[10.5259711,52.2729262],[10.5259526,52.2729234],[10.5258101,52.2732746],[10.5258697,52.2732837],[10.5260636,52.2733132],[10.5261371,52.2733243],[10.5262746,52.2729854],[10.5262888,52.2729876],[10.526312,52.2729304],[10.5262636,52.2729231],[10.5262239,52.272917],[10.5263222,52.2726767],[10.5261828,52.2726556]],[[10.5260636,52.2733132],[10.5260595,52.2733365],[10.5260575,52.2733486],[10.5258607,52.2733326],[10.5258631,52.2733195],[10.5258697,52.2732837],[10.5260636,52.2733132]]]
其中似乎包含多个多边形。
如何反转此多维数组的每个条目?
答案 0 :(得分:1)
假设array
是您的数据,请尝试以下内容:
var reversed = array.map(function reverse(item) {
return Array.isArray(item) && Array.isArray(item[0])
? item.map(reverse)
: item.reverse();
});
这是vanilla JavaScript。我认为在jquery中会是这样的:
var reversed = $.map(array, function reverse(item) {
return $.isArray(item) && $.isArray(item[0])
? $.map(item, reverse)
: item.reverse();
});
我没有尝试,但应该同时适用它们。让我知道。
答案 1 :(得分:1)
array=[[1,2,3],[4,5,6],[7,8,9]]
reverse_2D_array=array.map(row=>row.reverse()).reverse()
reverse_2D_array=[[9,8,7],[6,5,4],[3,2,1]]
<块引用>
注意:这个方法会改变原来的数组。
答案 2 :(得分:0)
<script type="text/javascript">
//given array
var arr = [
[
[10.5261828, 52.2726556],
[10.5263222, 52.2726767],
[10.5263578, 52.2726821],
[10.5263637, 52.2726677],
[10.5263738, 52.2726428],
[10.5264042, 52.2725678],
[10.526186, 52.2725346],
[10.5261395, 52.272649],
[10.5261828, 52.2726556]
],
[
[10.5261828, 52.2726556],
[10.5261713, 52.2726821],
[10.5261621, 52.2727047],
[10.5259248, 52.2726687],
[10.5257879, 52.2726479],
[10.5257435, 52.2727573],
[10.5258014, 52.2727661],
[10.5257967, 52.2727777],
[10.5260173, 52.2728113],
[10.5261107, 52.2728254],
[10.5260641, 52.2729403],
[10.5259711, 52.2729262],
[10.5259526, 52.2729234],
[10.5258101, 52.2732746],
[10.5258697, 52.2732837],
[10.5260636, 52.2733132],
[10.5261371, 52.2733243],
[10.5262746, 52.2729854],
[10.5262888, 52.2729876],
[10.526312, 52.2729304],
[10.5262636, 52.2729231],
[10.5262239, 52.272917],
[10.5263222, 52.2726767],
[10.5261828, 52.2726556]
],
[
[10.5260636, 52.2733132],
[10.5260595, 52.2733365],
[10.5260575, 52.2733486],
[10.5258607, 52.2733326],
[10.5258631, 52.2733195],
[10.5258697, 52.2732837],
[10.5260636, 52.2733132]
]
];
var arrModified = new Array();
var intDepth = 0;
//array depth calculation
function arrayDepth(arr) {
for(item in arr) {
if (Object.prototype.toString.call(arr) === '[object Array]') {
intDepth++;
arrayDepth(item);
}
}
}
arrayDepth(arr);
//for depth 1
if (intDepth == 1) {
arrModified = arr.reverse();
}
//for depth 2
if (intDepth == 2) {
for (item in arr) {
arrModified[item] = arr[item].reverse();
}
}
//for depth 3
if (intDepth == 3) {
for (i in arr) {
var arrTemp = arr[i];
var arrTemp1 = new Array();
for (j in arrTemp) {
arrTemp1[j] = arrTemp[j].reverse();
}
arrModified[i] = arrTemp1;
}
}
//so on for other depths
//arrModified is the required array
</script>