-
Notifications
You must be signed in to change notification settings - Fork 1
Loops
fire.js supports loop expressions that will execute the input as many times as necessary and return an array with the results.
All loops sets a special Zero-based variable called CurrentIndex
with the current iteration number.
@loop
executes the input forever until it found a @break
or @continue
.
The following loop will create an array with 29 numbers using CurrentIndex variable and @break
:
{
"name": "Examples.30Numbers",
"json": {
"@loop": {
"@equals": [
{
"@get(CurrentIndex)": null
},
30
],
"@if": {
"@break":null
},
"@get(CurrentIndex)": null
}
}
}
Download examples/Examples.30Numbers.fjson and execute to see the results:
$ firejs Examples.30Numbers.fjson
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
Notice how we compare CurrentIndex with the number 30 to known when to break the loop.
Loops also accepts a hint as the prefix for the names of the special variables.
The next example will skip the number 0 using CurrentIndex variable and @continue
, it also features a named loop so the variable names adopts the name as a prefix:
{
"name": "Examples.NoZeroNumbers",
"json": {
"@loop(mainLoop)": {
"@equals": [
{
"@get(mainLoopCurrentIndex)": null
},
30
],
"@if": {
"@break":null
},
" @equals": [
{
"@get(mainLoopCurrentIndex)": null
},
0
],
" @if": {
"@continue": null
},
"@get(mainLoopCurrentIndex)": null
}
}
}
Download examples/Examples.NoZeroNumbers.fjson and execute to see the results:
$ firejs Examples.NoZeroNumbers.fjson
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
@each
works executing the input per item in the array or until it found a @break
or @continue
. The array has to be the last result of the expression-block. Additionally to CurrentIndex
, @each
will also declares CurrentItem
so you can have access to the current iterated item of the array.
The following example will return an array of contacts based in an array of emails:
{
"name": "Examples.ContactsFromEmails",
"json": {
"@return": ["[email protected]", "[email protected]", "[email protected]"],
"@each(superheroes)": {
"email": {
"@get(superheroesCurrentItem)": null
}
}
}
}
Download examples/Examples.ContactsFromEmails.fjson and execute to see the results:
$ firejs Examples.ContactsFromEmails.fjson
[{"email":"[email protected]"},{"email":"[email protected]"},{"email":"[email protected]"}]
Thanks for Reading. Please take your time to read other tutorials.
- Johan(author) - [email protected]
If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.