question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For loop expression index / context?

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
larsgacommented, Apr 25, 2020

Interestingly, SAP’s example integration uses zip and range 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.jslt

Sounds like another vote in favour of either zip+range or zip-with-index

2reactions
larsgacommented, Apr 9, 2020

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

{ "rows" : [ for (range(size(.rows))) {
    "name" : .rows[.].name,
    "rowNo" : . + 1
}]}

Magic variable

That’s basically what you already showed.

Magic function

{ "rows" : [ for (.rows) {
    "name" : .name,
    "rowNo" : current-for-index() + 1
}]}

Opinions welcome.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found