1

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]]
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 11, 2023 at 22:44

1 Answer 1

3

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]
answered Nov 12, 2023 at 6:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.