Loops/Foreach: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Note and examples about iteration order and sorting of dictionary keys) |
(→{{header|jq}}: range and keys) |
||
Line 964: | Line 964: | ||
produces in turn 1 and 2. |
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 ".[]": |
|||
⚫ | |||
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) |
|||
⚫ | |||
produces: |
|||
[["a",1],["b",2]] |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |