如何对具有相同键值对的对象数组进行分组

How to group array of objects with same key value pair

我有一组具有相同键值对的字典。

[
  { "amount": "10" }, 
  { "amount": "20" },
  { "amount": "30" },
  { "amount": "20" },
  { "amount": "10" },
  { "amount": "10" }
]

我需要根据相同的键值对其进行分组。

预期样本结果:

有 3 个 10 码、2 个 20 码和 1 个 30 码

如何实现?

let array = [ ["amount": "10"], ["amount": "20"], ["amount": "30"], ["amount": "20"], ["amount": "10"], ["amount": "10"] ]
var result: [String: Int] = [:]
let key = "amount"
array.forEach {
    guard let value = [=10=][key] else { return }
    result[value, default: 0] += 1
}

print("\(result["10"])") // 3