had a case where i needed to check this byt for multiple elements. seems like a perfect case for the contains function:
$ jq --args 'contains ($ARGS.positional)' foobar bazbeq <<< '["foobar", "bazbeq", "xxx"]'
true
$ jq --args 'contains ($ARGS.positional)' foobar bazbeq yyy <<< '["foobar", "bazbeq", "xxx"]'
false
but there's an issue: contains checks the containment also for strings. so you have to watch out for substrings:
$ jq --args 'contains ($ARGS.positional)' foo baz <<< '["foobar", "bazbeq", "xxx"]'
true
Solutions I came up with:
wrapper + contains
Just wrap each elem in some "delimiter" - can be whatever
$ jq --args '[.[] | "#\(.)#"] | contains ([$ARGS.positional[] | "#\(.)#"])' foo bar baz beq <<< '["foobar", "bazbeq", "xxx"]'
false
$ jq --args '[.[] | "#\(.)#"] | contains ([$ARGS.positional[] | "#\(.)#"])' foobar bazbeq <<< '["foobar", "bazbeq", "xxx"]'
true
any + all
$ jq --args '[$ARGS.positional[] as $X | [$X == .[]] | any] | all' foo bar baz beq <<< '["foobar", "bazbeq", "xxx"]'
false
$ jq --args '[$ARGS.positional[] as $X | [$X == .[]] | any] | all' foobar bazbeq <<< '["foobar", "bazbeq", "xxx"]' true
index + all
$ jq --args '[ . | index ($ARGS.positional[]) ] | all' foo bar baz beq <<< '["foobar", "bazbeq" , "xxx", "yyy"]'
false
$ jq --args '[ . | index ($ARGS.positional[]) ] | all' foobar bazbeq <<< '["foobar", "bazbeq" , "xxx", "yyy"]'
true