5 ms·
I love these - I use jq daily and am always happy to read about it from another angle. The author probably has internalized more of the manual than they realiz
by phnofive 5y ago
I love these - I use jq daily and am always happy to read about it from another angle.
The author probably has internalized more of the manual than they realize, and maybe improved at least one explanation; FTA:
> map(...) let’s you unwrap an array, apply a filter and then rewrap the results back into an array. You can think of it as a shorthand for [ .[] | ... ] and it comes up quite a bit in my experience, so it’s worth it committing to memory.
From https://stedolan.github.io/jq/manual/#map(x),map_values(x) https://stedolan.github.io/jq/manual/#map(x),map_values(x) :
> map(x) is equivalent to [.[] | x]. In fact, this is how it's defined. Similarly, map_values(x) is defined as .[] |= x.
Note here the casual introduction of the update assignment operator, '|='
- jcims 5y agoWhat does [.[] | x] do?
- swader999 5y ago.map(x)
- jcims 5y agoThis is the most jq-ish reply xD
- Varriount 5y agoTake each sub-item in `.` (the current item being processed), apply the function `x` to each sub-item, and collect all the result values from `x` into an array.
- prutschman 5y agoNot a jq expert, but my understanding is: `.[]` takes a list and turns it into a sequence consisting of each element of that list. `| x` applies the filter `x` to that sequence, turning it into a new sequence. The outermost `[ ]` builds a list from that new sequence.