Array: Difference between revisions

→‎Immutability: [][m] = null
(→‎Immutability: [][m] = null)
Line 65:
jq offers a comprehensive collection of operators and functions for array processing, but to understand them it must be appreciated that all data values in JSON are immutable. In particular, if a is an array, a jq expression such as "a[0] = 1" may give the appearance of updating the array, but it simply returns an array identical to <tt>a</tt> except for the first element.
 
In fact, an expression such as "a[m] = 1" (where m is some non-negative integer) does not require that the length of the array, a, be at least (m+1). The expression should instead be interpreted to mean: produce an array derived from <tt>a</tt> such that a[m] == 1. If necessary, jq will add "null" elements to achieve this requirement. Thus, one way to create an array of m+1 nulls is to write:<lang jq>[][m] = null</lang>
It might seem that jq must be horrendously inefficient because of immutability, but in fact, jq is fast because of compiler optimizations.
 
2,503

edits