Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Get automated names for JS object keys

I'm building a JS script in GoogleEarthEngine. I am automating the code for many year to get a vegetation index (Enhanced Vegetation Index (EVI)) for each year. The code I'm working itself is much more complex, that's why I just added one here for this question.(code here).

I'm trying to get the name of the layer in the key of the JS Object. So it would be:

buffer_size: 500
class: 0
EVI_2021_mean: MEAN_VALUE_FOR_THIS_YEAR

and have in the end, other columns following the same idea, only changing the year value in the Key of the object and its mean value for the Value of the object

The format itself is important to be that way so I can export to KML it afterwards and move on with further analysis.

Instead, what I'm getting is the string 'key' as the Key and the string EVI_2021_mean as the Value.

features: List (4 elements)
0: Feature 0 (Polygon, 3 properties)
type: Feature
id: 1
geometry: Polygon, 24 vertices
properties: Object (3 properties)
    buffer_size: 500
    class: 0
    key: EVI_2021_mean

Obs: I'm setting the mean value inside the GetMean function:

var GetMean = function (fc, img, nome_img) {
  print(nome_img);
  
  var dict = [];
  var ZS_mean =  img.reduceRegion({
    reducer: ee.Reducer.mean()
    ,geometry: POI
    ,scale: 30
  });
  dict.push({
    key:   nome_img+'_'+'mean',
    value: ZS_mean.constant
  });
  print(ZS_mean);

  var SetMean = function(f) {
    return f.set(dict[0]);
  };
  
  return POI.map(SetMean);
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can set dynamic keys for JavaScript objects using the following notation:

const myKey = 'someKey'
const myObj = {
    [myKey]: 'myValue'
}

console.log(myObj)  // => { someKey: 'myValue' }

Additionally, you can set a dynamic property on an existing object like so:

const myKey = 'someKey'
const myObj = {}

myObj[myKey] = 'myValue'

console.log(myObj)  // => { someKey: 'myValue' }

I'd suggest modifying your code to make dict an object (like dict = {}) and where you dict.push({ ... }) to do like dict[nome_img+'_mean'] = ZS_mean.constant.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!