我有以下内容:
{"arr":[{"Name":"web"},{"Name":"app"}]}
我需要找到属性“Name”中没有“app”的所有数组对象的数组索引。
我试图使用“选择”和“键”的组合,但这不起作用:
jq '.arr[] | select(.Name != "app") | keys'
这是许多可能性中的一种:
.arr | range(0;length) as $i | select(.[$i].Name != "app") | $i
稍微简短但效率较低:
.arr | to_entries[] | select(.value.Name != "app") | .key
如果你渴望使用for-style循环,或者你想要想一些事情:
foreach .arr[] as $o (-1; .+1; select($o.Name != "app"))
这是一个使用tostream的解决方案。
tostream
| if .[0][-1] == "Name" and .[1] == "app"
then .[0][-2]
else empty
end