2

I would like to iterate a map function over several list-of-lists. Here is the code I run:

var feature1 = function(h) {
    var FCol= ee.FeatureCollection(ee.List(losslist.get(h)).map(function(element){
        element = ee.Dictionary(element); // cast
         var list = ee.List(element.get('groups'));
         var feats = ee.FeatureCollection(list.map(function(listEl){
         var dict = ee.Dictionary(listEl);
     return ee.Feature(null, dict);
  }));
   return feats
}))};

var myList = ee.List([lossperelevation,lossperslope,lossperyear]);
var listNumbers = ee.List.sequence(0,2)
var result = listNumbers.map(feature1); 

As an output, I get an error: User-defined methods must return a value. What I understood is that I need to add another return line. I do not understand what it should return, as the output I need is feats.

0

I recommend always making sure that you have consistent indentation and line breaking of your code, as this makes it easier to see which lines belong to which functions. This helps you (because you can learn to recognize and write correct patterns) and people you're asking for help (because standard formatting is easier for them to understand quickly).

Reformatted with some extra levels of nesting and comments, you have:

var feature1 = function(h) {
  var FCol = ee.FeatureCollection(
    ee.List(losslist.get(h)).map(function(element) {
      element = ee.Dictionary(element); // cast
      var list = ee.List(element.get('groups'));
      var feats = ee.FeatureCollection(
        list.map(function(listEl) {
          var dict = ee.Dictionary(listEl);
          return ee.Feature(null, dict);
        })                                // end of list.map
      );
      return feats;
    })                                    // end of losslist.get.map
  );
};                                        // end of feature1

It is now visible that the function feature1 has no return statement. Add return FCol; before the end of it to fix that.

Or, you can also replace var FCol = with return, so you're directly returning the value instead of assigning it to a variable and then immediately returning the variable.

1
  • Thank you for the recommendation and answer! – youcodegirl Feb 25 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.