As input, I receive two types of arrays of arrays made of x and y coordinates that represent polygon and multipolygon geometries.
array1
represents coordinates of a simple polygon geometry and array2
represent a multipolygon geometry:
var array1 = [[[0 , 0], [0, 1], [0 ,2]]]
var array2 = [[[[0, 0] , [0, 1], [0, 2]], [[1, 0], [1, 1], [1 ,2]]]]
Multipolygon geometry (array2
) are represented by an array of arrays one level deeper than simple polygon geometry (array1
).
I want to flatten those arrays in order to get those output:
if input is array1 type : array1_out = [[0, 0, 0, 1, 0, 2]]
if input is array2 type : array2_out = [[0, 0, 0, 1, 0, 2], [1, 0, 1, 1, 1, 2]]
My function is the following:
for (i=0; i < array_input.length; i++){
var array_concat = array_input[i].reduce(function(a, b){
return a.concat(b);
});
}
With this function above, the output for array1
is correct but the output of array2
is the following:
[[[0, 0] ,[0, 1], [0, 2]], [1, 0], [1, 1], [1, 2]]]
Is there a function to deeply flatten those two types of arrays?
Edit: Performance really matter here as the arrays are really big