In Google Earth Engine Developer\'s Guide, there is a recommendation to avoid for()
loops. They recommend to use map()
function as this example:
Assuming that you are just trying to understand GEE's map()
function, and how would be the equivalent of a normal js for loop
, the code would be:
var map_m = function(i) {
i = ee.Number(i)
var years = ee.List.sequence(2000, 2017)
var filtered_col = years.map(function(j) {
var filtered = modis.filter(ee.Filter.calendarRange(i, i, 'month'))
.filter(ee.Filter.calendarRange(j, j, 'year'))
return filtered
})
return filtered_col
}
var months = ee.List.sequence(1, 12)
var modis_list2 = months.map(map_m).flatten()
This code replicates a normal for loop
. First, it goes item by item of the years list, and then item by item of the months list, and then, once you have year and month, filter the collection and add it to a list (map
does that automatically). As you use 2 map
functions (one over years and the other over months), you get a list of lists, so to get a list of ImageCollection
use the flatten()
function. Somehow the printed objects are a bit different, but I am sure the result is the same.