I'm trying to reorganize some outputs from an EE array to an EE list. Specifically, I need to return a list of values from sublists of the array at the same index of each sublist. In my code below, I'd like to print an EE list object equal to [2, 5, 8]. The array slice returns a list of lists with the correct values, but I get the error under the code snippet when I try to flatten it. Is flatten the method to use here?
var arr = ee.Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
var arr_slice = ee.List(arr.slice(1,1,2));
var flat = arr_slice.flatten();
print(flat);
List.flatten, argument 'list': Invalid type.
Expected type: List<Object>.
Actual type: Short<0, 255, dimensions=2>.
Actual value: [[2], [5], [8]]
1 Answer 1
It seems casting using ee.List()
is not enough in this example. However, you can use ee.Array.toList()
instead:
var arr = ee.Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
print(arr);
var arr_slice = arr.slice(1,1,2).toList();
print(arr_slice)
var flat = arr_slice.flatten();
print(flat);
Output:
[[1,2,3],[4,5,6],[7,8,9]]
[[2],[5],[8]]
[2,5,8]
Explore related questions
See similar questions with these tags.