6

I'm using jq and trying to remove an element from an array based on it's value by can't figure out the syntax, it works with map but not del:

input = [10,11,12]

echo $input | jq -r 'map(select(. == 10))' returns [10]

echo $input | jq -r 'del(select(. == 10))' returns [10,11,12] not [11,12] as expected

Can someone point me in the right direction?

peak
  • 105,803
  • 17
  • 152
  • 177
twiglet
  • 63
  • 1
  • 3

1 Answers1

12

del is intended for deleting-by-path, not by-value:

 [10,11,12] | del(.[0]) #=> [11,12]

One way to achieve what you want is to use select:

 [10,11,12] | map(select(. != 10))

Another would be to use array subtraction:

 [10,11,12] - [10]

But that's maybe too easy.

peak
  • 105,803
  • 17
  • 152
  • 177