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中(多维)数组中的反向条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)