Loops in Imba behaves similar to array comprehensions in CoffeeScript and Python. They are expressions, and can be returned and assigned. When used as expressions they will always return an array (like Array#map, but with a few additional powerful features, like break / continue).
tag app-root
<self>
<ul> for item in ['Imba', 'Svelte', 'Javascript']
<li> item
# iterate through keys of object
for key,value of {a: 1, b: 2}
console.log key + ": " + value * 2
# prints:
# a: 2
# b: 4
while true
break if Math.random < 0.1
let counter = 0
until Math.random() < 0.1
counter++
# looping with index argument
for item, i in [1,2,3]
console.log i + ": " + item * 1
# prints:
# 0: 1
# 1: 2
# 2: 3
# loops are expressions
var list = [1,2,3,4,5]
var doubles = for num in list
num * 2
console.log doubles
# prints:
# [2,4,6,8,10]
# go through every other element
for item in [1,2,3,4,5] by 2
item * 2
console.log item
# prints:
# 1
# 3
# 5
# filter by condition
let list = [1,2,3]
for num in list when num > 1
console.log num
# prints:
# 2
# 3
Tip! Any type of object can support being iterated with forin in Imba. If the compiler does not know that the target is an array (at compile-time) it will look for (and call)
target.toArray
if it exists, and then loop through this.
The following example will render the names of the keys along with the values of the properties of each object.
let users = [
id: 1
name: "Eric"
hobbies: [
"tennis"
" piano"
]
---
id: 2
name: "Joe"
hobbies: [
"none"
]
]
tag app-root
def render
<self>
for user in users
<div>
<h3> user.name
<ul> for own key, item of user
<li> "{key}: {item}"
# loop over all keys of object
var object = {a: 1, b: 2, c: 3, d: 4}
for k,value of object
console.log "{k} 👉 " + value
# prints:
# a 👉 1
# b 👉 2
# c 👉 3
# d 👉 4
Continue without Arguments
var result = for num in [1,2,3,4,5]
continue if num == 3
num * 2
console.log result
# prints
# [2,4,8,10]
Continue with an Argument
var result = for num in [1,2,3,4,5]
continue -1 if num == 3
num * 2
console.log result
# acts like early return within map
# prints:
# [2,4,-1,8,10]
Break without arguments
var result = for num in [1,2,3,4,5]
break if num is 3
num
console.log result
# prints:
# [1,2]
Break with Argument
var result = for num in [1,2,3,4,5]
break 'done' if num is 3
num
console.log result
# prints:
# [1,2, "done"]