You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JQ删除JSON指定数组元素及对应索引的另一数组元素

JQ解决方案:同步过滤配对数组

针对你需求的完整JQ命令如下:

.[].searchMetaData |= if .status == "complete" then
  (.rrStandard | to_entries) as $rrEntries
  | (.ccStandard | to_entries) as $ccEntries
  | [$rrEntries[], $ccEntries[]]
  | transpose
  | map(select(.[0].value | startswith("XYZ")))
  | {
      rrStandard: map(.[0].value),
      ccStandard: map(.[1].value),
      status: .status
    }
else . end

步骤拆解

  • 用to_entries将两个数组转换为带索引的条目结构,每个元素包含key(索引)和value(元素值)
  • 合并两个条目数组后通过transpose按索引配对,得到[rr条目, cc条目]形式的数组
  • 过滤出rr条目值以XYZ开头的配对项
  • 分别提取过滤后配对项的rr和cc值,重新构建目标数组,同时保留原status字段

简化版写法(更紧凑)

也可以直接通过索引范围处理,代码更简洁:

.[].searchMetaData |= if .status == "complete" then
  (range(0; .rrStandard | length) | select(.rrStandard[.] | startswith("XYZ"))) as $indices
  | {
      rrStandard: $indices | map(.rrStandard[.]),
      ccStandard: $indices | map(.ccStandard[.]),
      status: .status
    }
else . end

说明

两种方法都能保证两个数组同步过滤,仅保留rrStandard中符合条件元素对应的ccStandard元素,生产环境中无论数组长度多少,只要两数组长度一致即可正常运行。

内容的提问来源于stack exchange,提问作者Rogelio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:52:08