如何从字典数组中删除重复的字典。
回复:
[
{
"voter_id":1
},
{
"passport":1
},
{
"pan_card":1
},
{
"aadhaar_card":1
},
{
"voter_id":1
},
{
"aadhaar_card":1
}
]
我们需要类似下面的输出
[
{
"passport":1
},
{
"pan_card":1
},
{
"aadhaar_card":1
},
{
"voter_id":1
}
]
我正在尝试此链接,但无济于事。
答案 0 :(得分:3)
只需将[String: Int]
的数组转换为Set
var foo = [
[
"voter_id":1
],
[
"passport":1
],
[
"pan_card": 1
],
[
"aadhaar_card":1
],
[
"voter_id":1
],
[
"aadhaar_card":1
]
]
Set(foo) // Usage
答案 1 :(得分:1)
如果要删除重复的密钥,请尝试此操作
func removeDuplicate(list: [[String:Any]]) -> [[String:Any]] {
var alreadyKnowKeys: [String] = []
var newArray: [[String:Any]] = []
list.forEach { (item) in
if let key = item.keys.first {
if !alreadyKnowKeys.contains(key) {
newArray.append(item)
alreadyKnowKeys.append(key)
}
}
}
return newArray
}