Pedro Rincon
Pedro Rincon

Reputation: 3

Assign values to variables in loop

I'm using the platform of Google Earth Engine.

I have my list of variables, and what i want is to assign each value of i to each variable

var Y03, Y04, Y05, Y06, Y07, Y08,Y09, Y10, Y11, Y12,Y13, Y14, Y15,Y16, Y17, Y18;

var Years = [ Y03, Y04, Y05, Y06, Y07, Y08,Y09, Y10, Y11, Y12,Y13, Y14, Y15,Y16, Y17, Y18];

for (var i = 2003; i <= 2018; i++){
  Years[i] = i; 
}
print(Y05); //undefined

As you can see, when i use print(Y05), the result is undefined and i want 2005.

Upvotes: 0

Views: 58

Answers (2)

palaѕн
palaѕн

Reputation: 73966

You can easily assign values to variables using Array destructuring like:

var [Y03, Y04, Y05, Y06, Y07, Y08,Y09, Y10, Y11, Y12,Y13, Y14, Y15,Y16, Y17, Y18] 
  = Array.from({length: 16}, (x, i) => 2003 + i)

console.log( Y05 )
console.log( Y03, Y18 )

Upvotes: 2

M Shahzaib Shoaib
M Shahzaib Shoaib

Reputation: 324

You can not assign values to variable in this way. If you print(Years) than you can see the updated values

Upvotes: 0

Related Questions