For loop expression index / context?
See original GitHub issueHi, I was wondering if it is possible to get the index of the current item when doing some transformation inside a “for” expression?
For example for the input:
{ "rows" : [
{"name":"test"},
{"name":"test 2"}
]}
I would like to add row number entries like:
{ "rows" : [
{"name" : "test", "rowNo" : 1},
{"name" : "test 2", "rowNo" : 2}
]}
I was able to achieve this using functions like this (functions are borrowed from issue #122):
let rows = .rows
def index-of(array, value)
find-index-of($array, $value, 0)
def find-index-of(array, value, index)
if ($index >= size($array))
-1
else if ($array[$index] == $value)
$index
else
find-index-of($array, $value, $index + 1)
{ "rows" : [ for (.rows) {
let rn = index-of($rows, .) + 1
"name":.name,
"rowNo": $rn
}]}
However, I wonder if there is a better way or if some variable (automatically set to proper value) could be added to for expression automatically to achieve same goal with simpler JSTL code, for example:
{ "rows" : [ for (.rows) {
"name":.name,
"rowNo": $forIdx + 1
}]}
Thanks 😃
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top Results From Across the Web
How to Use the Loop Expression in After Effects
Today we're talking about one of the most useful expressions in After Effects, the loop expression. This tutorial and article will share everything...
Read more >How to iterate a loop with index and element in Swift
As of Swift 3.0, if you need the index for each element along with its value, you can use the enumerated() method to...
Read more >The scope of index variables in Python's for loops
The for-loop makes assignments to the variables(s) in the target list. [...] Names in the target list are not deleted when the loop...
Read more >FOR LOOP Statement
With each iteration of the FOR LOOP statement, its statements run, its index is either incremented or decremented, and control returns to the...
Read more >Accessing the Index in a `for` Loop in Python | Sentry
Python's for loops are actually foreach loops. A foreach loop makes the code simpler to read but it maintains no counters. So rather...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Interestingly, SAP’s example integration uses
zip
andrange
to do exactly this: https://github.com/SAP-samples/cloud-workflow-samples/blob/master/cf-outlook-integration-sample/src/main/resources/com/sap/bpm/wfs/forms/adaptivecards/transform.jsltSounds like another vote in favour of either
zip
+range
orzip-with-index
I kind of expected this feature request to show up at some point. I guess there are three main ways this could be done.
Range function as in Python
Magic variable
That’s basically what you already showed.
Magic function
Opinions welcome.