Google Earth Engine JavaScript API – Iterating Over GEE Cloud Assets in JavaScript

google-earth-enginegoogle-earth-engine-javascript-apiiterationlist

I'm trying to to make my javascript Google Earth Engine code iterable, in the sense that I run the code once and it iterates through all of my cloud assets in a given folder. Each cloud asset represents an uploaded shapefile boundary- I want the code to automatically create new files clipped to all of my cloud assets. As I have hundreds of assets, this would take a long time to do manually.

I can create a list of all my cloud assets in a given folder using:

var assets = ee.data.listAssets('projects/FOLDER NAME/assets')
print(assets)

Printing the list produces an Object of tables. The properties "id" and "name" are the full name of the asset in its folder. However, I can't seem to be able to index this Object either through using the property id or by trying to isolate even the 1st item in the list. Whenver I do I don't get an error but the console simply outputs "undefined." If I could isolate the "id" or "name" properties from the Object, I could create a simple for loop where I could iterate through all of my assets.

I can turn this list into a dictionary var dict = ee.Dictionary(assets) and then isolate the "ga" property using var dict2 = dict.get('ga'), but this seems to make matters worse by increasing the size of the hierarchical structure of the Object. When I turn the list into a dictionary, the only key in the dictionary is "ga", which isn't helpful in solving my problem.

The closest related code I've been able to find is for GEE Python from 2018, but it uses a deprecated function (ee.data.getList).

Iterate over GEE assets in Python script and mixing local and server side objects

def iterate_over_asset(assetId):
    assets = ee.data.getList({'id':assetId})
    for asset in assets:
        asset_type = asset['type']
        asset_id = asset['id']
        if asset_type == 'ImageCollection':
            collection = ee.ImageCollection(asset_id)
            # do something with collection
        elif asset_type == 'Image':
            image = ee.Image(asset_id)
            # do something with image

iterate_over_asset('ASSET_ID')

I also found this related code from 2021 which uses the Python API. While this code does not use deprecated functions, I haven't been able to get asset names when translating this code to JavaScript. Again, doing so simply produces a console output of "undefined."
What are available asset types in Google Earth Engine?

def get_assets(folder=None, asset_list = []):

    # set the folder
    folder = folder if folder else ee.data.getAssetRoots()[0]['id']
    
    # loop in the assets
    for asset in ee.data.listAssets({'parent': folder})['assets']:
        if asset['type'] == 'FOLDER':
            asset_list = get_assets(asset['name'], asset_list)
        else:
            asset_list += [asset]
    
    return asset_list

Best Answer

I found the answer in a Stack Exchange question from earlier this year.

How to open a list of images from an asset as an Image Collection?

The following code does what I was looking for:

var assetList = ee.data.listAssets("projects/FOLDER NAME/assets")['assets']
                    .map(function(d) { return d.name })
Related Question