ee.Dictionary.map
Map an algorithm over a dictionary. The algorithm is expected to take 2 arguments, a key from the existing dictionary and the value it corresponds to, and return a new value for the given key. If the algorithm returns null, the key is dropped.
Usage | Returns |
---|
Dictionary.map(baseAlgorithm) | Dictionary |
Argument | Type | Details |
---|
this: dictionary | Dictionary | |
baseAlgorithm | Algorithm | |
Examples
// A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).
var dict = ee.Dictionary({
B1: 182,
B2: 219,
B3: 443
});
/**
* Convert S2 surface reflectance units to native scale.
*/
function scale(key, value) {
return ee.Number(value).divide(1e4);
}
print('S2 surface reflectance in native units', dict.map(scale));
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
# A dictionary (e.g. results of ee.Image.reduceRegion of an S2 image).
dic = ee.Dictionary({
'B1': 182,
'B2': 219,
'B3': 443
})
def scale(key, value):
"""Convert S2 surface reflectance units to native scale."""
return ee.Number(value).divide(1e4)
print('S2 surface reflectance in native units:', dic.map(scale).getInfo())
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-13 UTC.
[null,null,["Last updated 2024-07-13 UTC."],[[["`Dictionary.map()` applies a provided algorithm to each key-value pair in a dictionary, transforming the values based on the algorithm's logic."],["The algorithm used by `Dictionary.map()` receives a key and its corresponding value as input, and it should return the new value for that key."],["If the algorithm returns null for a specific key, that key-value pair will be excluded from the resulting dictionary."],["`Dictionary.map()` is particularly useful for tasks like data transformation and filtering within dictionaries, offering a concise way to modify values based on custom logic."],["Example use cases include converting units of measurement within a dataset or selectively removing entries based on certain criteria applied to the key-value pairs."]]],["The core content details the `map` function for dictionaries. This function applies a user-defined algorithm to each key-value pair within a dictionary. The algorithm takes a key and its corresponding value as input and returns a new value. The `map` function iterates through the dictionary, executes the provided algorithm on each pair, and generates a new dictionary with the updated values. If the algorithm's result is `null`, the key is removed from the resulting dictionary. Examples illustrate this with scaling values in a dictionary.\n"]]