【问题标题】:Remove duplicate objects in JSON array with jq based on specific criteria根据特定条件使用 jq 删除 JSON 数组中的重复对象
【发布时间】:2021-07-15 03:29:12
【问题描述】:

我有以下数据,我希望根据 "run" 键的重复值删除整个对象,同时保留具有最大 "startTime" 编号的对象:

{
  "data": {
    "results": [
      {
        "event": {
          "biking": {
            "startTime": 12,
            "id": "a",
            "run": "x"
          }
        },
        "displayName": "Alex"
      },
      {
        "event": {
          "biking": {
            "startTime": 10,
            "id": "b",
            "run": "x"
          }
        },
        "displayName": "Adam"
      },
      {
        "event": {
          "biking": {
            "startTime": 11,
            "id": "c",
            "run": "y"
          }
        },
        "displayName": "Aaron"
      }
    ]
  }
}

我一直在尝试将uniquejq 联系起来,但无法完全得到我想要的。我的预期结果是这样的:

{
  "data": {
    "results": [
      {
        "event": {
          "biking": {
            "startTime": 12,
            "id": "a",
            "run": "x"
          }
        },
        "displayName": "Alex"
      },
      {
        "event": {
          "biking": {
            "startTime": 11,
            "id": "c",
            "run": "y"
          }
        },
        "displayName": "Aaron"
      }
    ]
  }
}

我试图使用unique,因为我只想保留每个"run": id 中的一个,而在更大的列表中,我可能有三个x、两个y 和四个z。在这种情况下,基于最大的"startTime",我想保留一个xyz

【问题讨论】:

  • 到目前为止您尝试了什么 - 请向我们展示您编写此代码的尝试。
  • 使用数组filter函数
  • 该问题已使用 jq 和 jquery 标签进行了标记,但它们之间几乎没有任何关系。如果你想要一个jQuery解决方案,请更改标题;否则,请删除 jquery 标签。谢谢。
  • 感谢您的提醒! jquery 标记已删除

标签: json unique jq


【解决方案1】:

这是一个简单的 jq 解决方案:

.data.results |=
  (group_by(.event.biking.run)
   | map(max_by(.event.biking.startTime)))

它使用group_by按“运行”分组,然后max_by选择所需的事件。

【讨论】:

  • 这正是我想要的 - 谢谢!
【解决方案2】:
  • 下面是使用 reducer 的方法。

const input = {
  "data": {
    "results": [{
        "event": {
          "biking": {
            "startTime": 12,
            "id": "a",
            "run": "x"
          }
        },
        "displayName": "Alex"
      },
      {
        "event": {
          "biking": {
            "startTime": 10,
            "id": "b",
            "run": "x"
          }
        },
        "displayName": "Adam"
      },
      {
        "event": {
          "biking": {
            "startTime": 11,
            "id": "c",
            "run": "y"
          }
        },
        "displayName": "Aaron"
      }
    ]
  }
};
const output = {
  data: {}
};
output.data.results = Object.values(input.data.results.reduce((r, o) => {
  r[o.event.biking.run] =
    (r[o.event.biking.run] &&
      r[o.event.biking.run].event.biking.startTime > o.event.biking.startTime) ? r[o.event.biking.run] : o
  return r
}, {}));
console.log(output);

【讨论】:

    【解决方案3】:

    这样就可以了

    const result = [
      ...new Map(
        obj.data.results.map((item) => [item["event"]["biking"]["run"], item])
      ).values()
    ];
    

    DEMO

    这是基于helprjsremoveDuplicates。

    【讨论】:

      猜你喜欢
      • 2014-03-14
      • 1970-01-01
      • 2017-01-07
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多