ee.List.iterate

Iterate an algorithm over a list. The algorithm is expected to take two objects, the current list item, and the result from the previous iteration or the value of first for the first iteration.
UsageReturns
List.iterate(function, first)Object
ArgumentTypeDetails
this: listList
functionAlgorithm
firstObject

Examples

Code Editor (JavaScript)

// This example uses the ee.List.iterate function to generate a series of
// sequentially halved values.

// Declare a list that will hold the series of sequentially halved values,
// initialize it with the starting quantity.
var quantityList = [1000];

// Define the number of iterations as a list sequence.
var nSteps = ee.List.sequence(1, 10);

// Declare a function that takes the current element of the iteration list and
// the returned result of the previous iteration as inputs. In this case, the
// the function is returning an accumulating list of quantities that are reduced
// by half at each iteration.
var halfOfPrevious = function(currentElement, previousResult) {
  var previousQuantity = ee.Number(ee.List(previousResult).get(-1));
  var currentQuantity = previousQuantity.divide(2);
  return ee.List(previousResult).add(currentQuantity);
};

// Apply the function to the nSteps list, each element is an iteration.
quantityList = ee.List(nSteps.iterate(halfOfPrevious, quantityList));

// Display the results. Note that step 0 is included for the initial value.
print('Steps in the iteration of halved quantities', nSteps);
print('Series of sequentially halved quantities', quantityList);
print(ui.Chart.array.values({
  array: quantityList,
  axis: 0,
  xLabels: ee.List([0]).cat(nSteps)
}));