Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq remove element from array by value

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?

like image 685
twiglet Avatar asked Jan 03 '18 13:01

twiglet


1 Answers

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.

like image 172
peak Avatar answered Sep 22 '22 01:09

peak