Loops/Foreach: Difference between revisions

→‎{{header|jq}}: range and keys
(→‎{{header|Python}}: Note and examples about iteration order and sorting of dictionary keys)
(→‎{{header|jq}}: range and keys)
Line 964:
produces in turn 1 and 2.
 
Sometimes it is necessary to use an alternative to ".[]. For example, one might want to generate an index along with the array. In such cases, the "range(m;n)" generator, which performs a similar role to C's "for(i=m; i<n; i++)", can be used. Here is how range/2 would be used to perform the task:
There are two notable alternatives to using ".[]":
<lang jq>example | recurserange( .[1:]0; example|length > 0 ) | example[.[0]</lang>
 
For JSON objects, the corresponding method involves using <tt>keys</tt>, e.g.
'''Using recurse/1''':
 
<lang jq>example | recurse(if length > 1 then .[1:] else empty end ) | .[0]</lang>
{"a":1, "b":2} | . as $o | keys | map( [., $o[.]] )
'''Using recurse/2''' (requires jq > 1.4)
 
<lang jq>example | recurse( .[1:]; length > 0 ) | .[0]</lang>
produces:
 
[["a",1],["b",2]]
 
=={{header|Lasso}}==
2,515

edits