I have a geojson with multiple arrays, such as:


I need to write some code, which iterates each array and switches both elements in each sub-array (are these called like that?)

The result should look like this:


closed as off-topic by Ian Turton Jun 21 '17 at 7:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

var oldArray = [[10.77932197941255,52.03584196145097],

var newArray = [];

for (var i = 0; i < oldArray.length; i++) {

  var oldCoordinate = oldArray[i];
  var newCoordinate = [oldCoordinate[1], oldCoordinate[0]];



"newArray" now has all the switched coordinates. See the fiddle here for testing: jsfiddle.net/8rLhj0o3/

  • another question: lets say i have a multipolygon and I want to iterate through all polygons like in your answer. when i try this, I only get one huge array with connected geometries. how would i do loop this correctly? – Revo Jun 21 '17 at 8:14
  • jsfiddle.net/dr0m62yn/1 – Revo Jun 21 '17 at 8:24
  • 1
    You need to run multiple for loops inside each other, depending on the data. See this fiddle with a working example: jsfiddle.net/xyg2Le47 – Zeronull Jun 21 '17 at 9:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.