google-earth-engine – Using Variable for Property Name in Google Earth Engine ee.Feature

google-earth-enginegoogle-earth-engine-javascript-apisyntaxerror

I would like to know if it is possible to set a property name using a computed variable in GEE.

For example, I create the following feature:

var feature = ee.Feature(null,{propertyName: 0.85});

and would like to be able to use a property name which was previously defined, so instead of using "propertyName" I would use var propertyName = 'propertyValue' .

I found this Stack Overflow answer relating to JavaScript in general which states that you may place the variable name in between brackets.

var foo = "bar";
var ob  = { [foo]: "something" }; // ob.bar === "something"

However trying the same notation in GEE only produced an error SyntaxError: Unexpected token.

Best Answer

var feature = ee.Feature(null).set(propertyName, 0.85);

This will also work if propertyName Earth Engine computed/server-side string values, not just JavaScript strings.