如何正确使用jq的in()函数

How to correctly use jq's in() function

我正在尝试了解 jqin() 功能。

https://jqplay.org/s/BR1KbCjP8u

filter:
map( in(["ms", "is", "bad"]) )
input:
["apple","is","bad"]

我期望输出 [false,true,true] 因为对于输入数组的每个元素:

  1. “apple”不在 [“ms”、“is”、“bad”] 中,所以错误
  2. “是”在 [“毫秒”、“是”、“坏”] 如此真实
  3. “坏”在 [“毫秒”、“是”、“坏”] 中如此真实

显然这是错误的,因为我得到错误:

jq: error (at <stdin>:0): Cannot check whether array has a string key
exit status 5

这有什么问题以及如何在过滤器中传递 ["ms","is","bad"] 时正确使用 in() 函数?我想检查输入数组中的每个元素是否在此列表中找到。

也许您正在寻找 IN(而不是 in)函数。此外,它需要一个元素流,而不是一个数组。

map(IN("ms", "is", "bad"))
[false,true,true]

Demo


来自manual:

in

The builtin function in returns whether or not the input key is in the given object, or the input index corresponds to an element in the given array. It is, essentially, an inversed version of has.

IN

This builtin outputs true if . appears in the given stream, otherwise it outputs false.

我使用 in() 不正确。

https://jqplay.org/s/FJcNTgplG6

filter:
map(in(["apple","is","bad"]))
input:
[3, 2, 1, 0]
output:
[
  false,
  true,
  true,
  true
]

正在测试是否在输入数组中找到过滤器数组的索引。