Skip to main content

for_in

The for...in loop is the most idiomatic way to iterate over collections in Soul. It provides a clean syntax for iterating through lists, maps, and other iterable objects without managing indices manually.

Basic For-In with Lists

Iterate through list elements:

For-In with Maps

Iterate through map key-value pairs:

For-In with Key Only (Maps)

Iterate through map keys only:

For-In with Value Only (Lists)

The standard pattern for lists:

For-In with Index and Value

Some collections may provide both index and value:

For-In with Nested Collections

Iterate through nested data structures:

For-In with String Characters

Iterate through string characters:

For-In with Break and Continue

Control loop execution:

For-In with Complex Data

Process complex data structures:

For-In with Filtering

Filter elements during iteration:

For-In with Transformations

Transform data during iteration:

For-In with Function Calls

Call functions for each element:

For-In with Maps and Complex Keys

Iterate through maps with complex structures:

For-In with Nested Loops

Nested for-in loops for multi-dimensional data:

For-In with Error Handling

Handle errors during iteration:

For-In with Conditional Logic

Complex conditional processing:

For-In with Accumulation

Accumulate values during iteration:

For-In Performance Tips

Optimize for-in loop performance:

Best Practices

  1. Use for-in for collections: It’s more readable than index-based loops
  2. Use meaningful variable names: user instead of u
  3. Handle null/undefined: Check for valid data before processing
  4. Use break/continue appropriately: Control flow when needed
The for...in loop is the preferred way to iterate over collections in Soul, providing clean, readable code that’s less error-prone than manual index management.