Google Earth Engine get single value area from group object

google-earth-engine

i want to compute area from a classification in Google earth engine so i use the group to compute it, then the result is like this

enter image description here

How can i get the single sum value from a class: 0 ? then define it as a new variable

Best Answer

Thats a dictionary containing 1 item ('groups') which is a list of dictionaries, each containing two items ('class' and 'sum').

var list = ee.List(ee.Dictionary(result).get('groups'))
var group0 = ee.Dictionary(list.get(0))
var sum = group0.get('sum')

But if you're going to do something with all of the groups, it might be better to convert the list of dictionaries into two lists of classes and sums:

var list = ee.List(result.get('groups'))
var classes = list.map(function(d) {
  return ee.Dictionary(d).get('class')
})
var sums = list.map(function(d) {
  return ee.Dictionary(d).get('sum')
})