0

I want to select elements >= 3 in an array like [2, 4, 3] with jq, how do I do it?

I have found answers when the array contains objects (e.g [{Name:"a", Age:2} ...]}) with things like select (.Age >= 2) but I don't know how to refer to values

peak
  • 105,803
  • 17
  • 152
  • 177
Thomas
  • 8,306
  • 8
  • 53
  • 92

1 Answers1

0

Use ..

If you want to retain the array structure, you could use map(select(_)), e.g.

jq -n '[2, 4, 3] | map(select(. >= 3))'

If you just want the values, you could consider:

jq '.[] | select(. >= 3)' <<< '[2, 4, 3]'
peak
  • 105,803
  • 17
  • 152
  • 177
  • works! I had good intuition actually and tried the `.` but didn't use `map`. Why does it work only with `map` ? I have seen `select` work alone before – Thomas Sep 29 '18 at 04:50
  • See the def of select at https://raw.githubusercontent.com/stedolan/jq/master/src/builtin.jq – peak Sep 29 '18 at 06:26