Loops

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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
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 or Array or through all properties in a Dictionary or Feature. See the examples below for more details.

Array

You can iterate through elements in arrays using the for...in statement. In this case, the variable definition in the loop represents the index of the array, not the value of the item itself.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
// 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).

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
var sensitiveFeatures = Filter($layer, "class = 'sensitive'");
var largeAreasPresent;

for(var f in sensitiveFeatures){
  largeAreasPresent = iif(f.ACRES > 5000, true, false);
}
return largeAreasPresent;

Break

The break keyword stops the loop from iterating any further.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
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).

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
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
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
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

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.