0

I'm trying to filter an array with another. For example, given I have this input array: ['foo', bar', 'baz'] And this filter array: ['foo', 'baz']

I want to have this output: ['bar']

I feel like I could be able to do this by piping to select(inside()), but I can't get inside() to work; I get a "not defined" error.

nVitius
  • 2,024
  • 15
  • 21

1 Answers1

1

You can use the convenient subtraction operator - as follows:

jq '. - ["foo", "baz"]'
Hans Z.
  • 50,496
  • 12
  • 102
  • 115
  • What if I wanted to do for values in an object instead? i.e. `[{"key": "foo"},{"key": "bar"},{"key": "baz"}]` I only really care about the values themselves, so I tried doing: `.[].key - ["foo", "baz"]` but that doesn't work. – nVitius Sep 01 '15 at 21:02
  • 1
    @nVitius: That's a completely different question all together. You stated that the input array is of strings, not objects. Either way, [that question](http://stackoverflow.com/questions/26701538) ([and](http://stackoverflow.com/questions/29659436) [its](http://stackoverflow.com/questions/25649960) [variations](http://stackoverflow.com/questions/29518137)) was already answered. – Jeff Mercado Sep 01 '15 at 23:03
  • Yeah, it is. Thanks for the links to the other questions. – nVitius Sep 02 '15 at 01:23