Array: Difference between revisions
Content added Content deleted
(→Streams and Arrays: ce) |
(→map/reduce: map(-(.))) |
||
Line 96: | Line 96: | ||
====map/reduce==== |
====map/reduce==== |
||
jq's "map" filter is quite conventional, e.g. <lang jq>[1,2,3] | map(-) # => [-1, -2, -3]</lang>The <tt>reduce</tt> filter, however, has a special syntax that makes it very powerful. In brief, the syntax is:<lang jq>reduce S as $var (init; update) |
jq's "map" filter is quite conventional, e.g. <lang jq>[1,2,3] | map(-(.)) # => [-1, -2, -3]</lang>The <tt>reduce</tt> filter, however, has a special syntax that makes it very powerful. In brief, the syntax is:<lang jq>reduce S as $var (init; update) |
||
</lang>where S is an expression producing a stream of values, and init and update are jq expressions. The reduction takes place by setting a hidden variable to the value of "init" initially, and then performing the update on the hidden variable as specified by "update" for each value, $var, in the stream. The result of the reduction is the final value of the hidden variable. |
</lang>where S is an expression producing a stream of values, and init and update are jq expressions. The reduction takes place by setting a hidden variable to the value of "init" initially, and then performing the update on the hidden variable as specified by "update" for each value, $var, in the stream. The result of the reduction is the final value of the hidden variable. |
||