Loops allow you to run a block of statements over and over again as long as a given condition with an incrementing value evaluates to true. They can also be used to iterate through items in an Array or a FeatureSet.
A loop is defined using the for
or while
keyword.
For
When declaring a for loop to run a block of code a specific number of times, you must include three statements:
- A variable definition. This must be a number.
- A condition that must evaluate to true at the start of the next execution of the block;
- An expression that either increments or decrements the variable defined in the first statement.
In the following example, the variable z
defined in the first statement of the for loop increments by 1 each time the loop begins another execution of the block. When z
reaches a value of 100, the execution of the block stops and the final result is returned.
var result = 0;
for(var z=0; z < 100; z++) {
result += z;
}
return result; // returns 4950
For...in
The for...in
statement allows you to iterate through all items in a FeatureSet, indices in an Array, or through all keys in a Dictionary or Feature. See the examples below for more details.
Array
You can iterate through indices in an array (not the array elements themselves) using the for...in
statement.
var myArray = [10,20,30];
var result = 0;
for(var index in myArray) {
// index represents the position of the element and must be used to
// access the values of an array in this manner
result += myArray[index];
}
return result; // returns 60
Dictionary
You may iterate through keys in a dictionary using for...in
. The value of the variable declared in the for
represents a key of the dictionary. To access the value of a key, you must use the square bracket syntax to refer to the key name.
var myDictionary = Dictionary("field1", 1, "field2", 2);
var n = 0;
for(var k in myDictionary) {
n += myDictionary[k]; // Note that k will be "field1" and then "field2"
}
return n; // returns 3;
Feature
You may also iterate through attributes in a feature similar to how you iterate through dictionary keys. When referencing attributes in a feature as a variable, you must declare all fields expected to be used by the expression using the Expects function. You can use the *
character to request data from all fields.
// Indicates all fields in the feature will be used in the expression
Expects($feature, "*");
var n = 0;
for(var attribute in $feature) {
n += $feature[attribute];
}
return n;
FeatureSet
The for...in
statement allows you to iterate through items in a FeatureSet. In this case, the variable declaration will represent a Feature (not its index).
var sensitiveFeatures = Filter($layer, "class = 'sensitive'");
var largeAreasPresent;
for(var f in sensitiveFeatures){
largeAreasPresent = iif(f.ACRES > 5000, true, false);
}
return largeAreasPresent;
For...of
The for...of
statement allows you to iterate through all items in an Array or through all property values in a Dictionary or Feature. See the examples below for more details.
Array
You can iterate through elements in arrays using the for...of
statement. In this case, the variable definition in the loop represents the element of the array, not the index.
var myArray = [10,20,30];
var result = 0;
for(var value of myArray) {
result += value;
}
return result; // returns 60
Dictionary
You may iterate through values in a dictionary using for...of
. The value of the variable declared in the for
represents a key-value pair in the dictionary.
var myDictionary = Dictionary("field1", 1, "field2", 2);
var n = 0;
for(var kv of myDictionary) {
// kv.value looks up the value
// kv.key tells us the key used to look up the value
n += kv.value; // Note that kv.value will be 1 and then 2
}
return n; // returns 3;
Feature
You may also iterate through attribute values in a feature similar to how you iterate through dictionary key-value pairs. When referencing attribute values in a feature, you must declare all fields expected to be used by the expression using the Expects function. You can use the *
character to request data from all fields.
// Indicates all fields in the feature will be used in the expression
Expects($feature, "*");
var n = 0;
for(var kv of $feature) {
// kv.value looks up the value
// kv.key tells us the attribute used to look up the value
n += kv.value;
}
return n;
Break
The break
keyword stops the loop from iterating any further.
var sensitiveFeatures = Filter($layer, "class = 'sensitive'");
var largeAreasPresent;
for(var f in sensitiveFeatures){
if(!(AreaGeodetic(f) > 0)){
console("There's a feature without valid data. Please correct it.");
break;
}
largeAreasPresent = iif(f.ACRES > 5000, true, false);
}
Use return
inside the loop to exit the loop and function (or script).
var sensitiveFeatures = Filter($layer, "class = 'sensitive'");
// script returns true once the condition is true. There is no more
// need to iterate through the rest of the features.
for(var f in sensitiveFeatures){
if(f.ACRES > 5000){
return true;
};
}
Continue
Continue allows you to skip executing statements in the block for only the current iteration.
var n = 0;
for(var i=1; i<100; i++) {
if (i % 2 == 1) {
continue;
}
n+=i;
}
return n;
// only executes the block for even numbers.
// returns 2450
var n = 0;
for(var i=1; i<100; i++) {
if (i % 2 == 0) {
continue;
}
n+=i;
}
return n;
// only executes the block for odd numbers.
// returns 2500
While
The while loop executes a block of code as long as the given condition is true.
Expects($feature, "*");
var prefix = "Year_";
var y = 1900;
var total = 0;
while(y <= 2020){
var fieldName = prefix + y;
if(HasKey($feature, fieldName)){
total += $feature[fieldName];
}
y+=10; // increment y so the while will eventually stop
}
return total;
Use caution when writing a while
loop as it can easily result in an infinite loop. This will occur if you fail to increment the variable used in the while condition.
var y = 1900;
var total = 0;
while(y <= 2020){
total += y;
}
return total;
// the while loop will never stop executing because the value of y never changes