javascript中(多维)数组中的反向条目

reverse entries in (multi-dimensional) array in javascript(javascript中(多维)数组中的反向条目)
本文介绍了javascript中(多维)数组中的反向条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 Leafletjs 在 openstretmap 上显示一些多边形.

I'm using leafletjs to display some polygons on an openstretmap.

我有一个外部数据资源,它为我提供了多边形的坐标.不幸的是,这个数组的坐标顺序错误.

I have an external data ressource which gives me the coordinates for the polygons. Unfortunately this array has the wrong order for the coordinates.

示例:我明白了:

[[10.5254913,52.2734311],[10.5258872,52.2734632]]

[[10.5254913,52.2734311],[10.5258872,52.2734632]]

我需要:

[[52.2734311,10.5254913],[52.2734632,10.5258872]]

[[52.2734311,10.5254913],[52.2734632,10.5258872]]

所以我给自己写了一个小函数,它遍历数组并反转条目:

So I wrote myself a little function which iterates through the array and reverses the entries:

var polCoords = [];
$.each(value.polygon[0], function(key,value){
polCoords[key] = [value[1],value[0]]; 
});

这很好用.但是现在我发现多边形的一些数组是多维的!所以我有一个这样的数组:

This works just fine. But now I discovered that some arrays for the polygons are multidimensional! So I have an array like this:

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

它似乎由多个多边形组成.

Which seems to consist of more than one polygon.

如何反转这个多维数组的每个条目?

How can I reverse every entry of this multidimensional array?

推荐答案

假设 array 是您的数据,尝试类似:

Assuming array is your data, try something like:

var reversed = array.map(function reverse(item) {
    return Array.isArray(item) && Array.isArray(item[0]) 
               ? item.map(reverse) 
               : item.reverse();
});

这是普通的 JavaScript.我认为在 jquery 中会是这样的:

This is vanilla JavaScript. I think in jquery would be something like:

var reversed = $.map(array, function reverse(item) {
    return $.isArray(item) && $.isArray(item[0])
               ? $.map(item, reverse)
               : item.reverse();
});

我没有尝试,但应该可以同时使用它们.告诉我.

I didn't try but should works both of them. Let me know.

这篇关于javascript中(多维)数组中的反向条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)