JQ——按值而不是索引删除元素

JQ — Remove elements by value not index

我需要删除 instances 中设置为“三”或“五”的所有元素。他们的索引并不总是相同的:

{
"address": "localhost",
"name": "local",
"vars": {
    "instances": [
        "one",
        "two",
        "three",
        "four",
        "five"
    ]
  }
}

假设你有:

[10, 10, 20, 10, 20, 10, 30]

…并且您想删除所有 20 和所有 30。这是一种方法:

. - [20, 30]

As well as normal arithmetic subtraction on numbers, the - operator can be used on arrays to remove all occurrences of the second array's elements from the first array.

https://stedolan.github.io/jq/manual/#Builtinoperatorsandfunctions

在你的情况下你可以这样做:

.vars.instances -= ["three", "five"]

…returns:

{
  "address": "localhost",
  "name": "local",
  "vars": {
    "instances": [
      "one",
      "two",
      "four"
    ]
  }
}