【问题标题】:recreate array with all items but the last使用除最后一项以外的所有项目重新创建数组
【发布时间】:2021-11-25 00:25:42
【问题描述】:

鉴于以下情况:

输入:

[
  [
    {
      "type": "strip",
      "output": "debian-7-x86_64-strip"
    },
    {
      "type": "compress",
      "output": "debian-7-x86_64-compress"
    },
    {
      "type": "full",
      "output": "debian-7-x86_64"
    }
  ],
  [
    {
      "type": "strip",
      "output": "debian-8-strip"
    },
    {
      "type": "compress",
      "output": "debian-8-compress"
    }
  ]
]

如何重新创建删除了最后一个对象项(来自每个子数组)的数组?

所需的输出:

[
  [
    {
      "type": "strip",
      "output": "debian-7-x86_64-strip"
    },
    {
      "type": "compress",
      "output": "debian-7-x86_64-compress"
    }
  ],
  [
    {
      "type": "strip",
      "output": "debian-8-strip"
    }
  ]
]

我尝试过使用 ... |= .[:-1]...[to_entries | last]... | select(last|not) 之类的东西,但没有成功。

非常感谢您的帮助。

【问题讨论】:

    标签: json jq


    【解决方案1】:

    .[][0:-1] 让您走得更远,只是缺少输出中的周围数组。

    编辑:[.[][:-1]] 应该这样做。

    【讨论】:

    • 太棒了,谢谢。
    • 或者只是:.[:-1]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2015-07-30
    相关资源
    最近更新 更多