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:
For JSON objects, the corresponding method involves using <tt>keys</tt>, e.g.
{"a":1, "b":2} | . as $o | keys | map( [., $o[.]] )
▲<lang jq>example | recurse( .[1:]; length > 0 ) | .[0]</lang>
produces:
[["a",1],["b",2]]
=={{header|Lasso}}==
|