检查字典数组的值是否与数组元素列表匹配
Check if a value of an array of dictionaries matches a list of array elements
我在 Swift 中有一个数组(项目)和一个字典数组(数据):
let items = [2, 6, 4]
var data = [
["id": "1", "title": "Leslie", "color": "brown"],
["id": "8", "title": "Mary", "color": "red"],
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "5", "title": "Stephanie", "color": "pink"],
["id": "9", "title": "Steve", "color": "purple"],
["id": "3", "title": "Doug", "color": "violet"],
["id": "4", "title": "Ken", "color": "white"],
["id": "7", "title": "Annie", "color": "black"]
]
我想创建一个数组,其中包含那些 "id" 等于 'items' 数组中提供的数字的字典数组。也就是我想最终得到一个数组:
var result = [
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "4", "title": "Ken", "color": "white"]
]
我曾尝试使用谓词,但在剧烈头痛和心脏骤停后,我并没有成功。对于这项任务,它们似乎异常复杂。我现在只想在一个简单的 for-in 循环中执行此操作。
是否有使用谓词或其他方法的巧妙方法?
像这样使用 filter
和 contains
:
let result = data.filter { dict in
if let idString = dict["id"], id = Int(idString) {
return items.contains(id)
}
return false
}
试试这个,应该有效:
let items = [2, 6, 4]
var data = [
["id": "1", "title": "Leslie", "color": "brown"],
["id": "8", "title": "Mary", "color": "red"],
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "5", "title": "Stephanie", "color": "pink"],
["id": "9", "title": "Steve", "color": "purple"],
["id": "3", "title": "Doug", "color": "violet"],
["id": "4", "title": "Ken", "color": "white"],
["id": "7", "title": "Annie", "color": "black"]
]
var result = [Dictionary<String, String>]()
for index in 0..<data.count {
let myData = data[index]
let dataID = data[index]["id"]
for i in 0..<items.count {
if dataID == "\(items[i])" {
result.append(myData)
}
}
}
for i in 0..<result.count {
print(result[i])
}
结果是:
谢谢大家的回复!我认为 Eric D 的解决方案最接近我所追求的。
我终于能够在 raywenderlich.com 上找到具有良好描述的解决方案:https://www.raywenderlich.com/82599/swift-functional-programming-tutorial
使用该方法(同样,非常接近 Eric D 的建议),首先我可以将其分解为:
func findItems(value: [String: String]) -> Bool {
return items.contains(Int(value["id"]!)!)
}
var result = data.filter(findItems)
我可以进一步减少到:
var result = data.filter {
(value) in items.contains(Int(value["id"]!)!)
}
同样,可以将其简化为一行,如:
var result = data.filter { items.contains(Int([=12=]["id"]!)!) }
再次感谢大家!
我在 Swift 中有一个数组(项目)和一个字典数组(数据):
let items = [2, 6, 4]
var data = [
["id": "1", "title": "Leslie", "color": "brown"],
["id": "8", "title": "Mary", "color": "red"],
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "5", "title": "Stephanie", "color": "pink"],
["id": "9", "title": "Steve", "color": "purple"],
["id": "3", "title": "Doug", "color": "violet"],
["id": "4", "title": "Ken", "color": "white"],
["id": "7", "title": "Annie", "color": "black"]
]
我想创建一个数组,其中包含那些 "id" 等于 'items' 数组中提供的数字的字典数组。也就是我想最终得到一个数组:
var result = [
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "4", "title": "Ken", "color": "white"]
]
我曾尝试使用谓词,但在剧烈头痛和心脏骤停后,我并没有成功。对于这项任务,它们似乎异常复杂。我现在只想在一个简单的 for-in 循环中执行此操作。
是否有使用谓词或其他方法的巧妙方法?
像这样使用 filter
和 contains
:
let result = data.filter { dict in
if let idString = dict["id"], id = Int(idString) {
return items.contains(id)
}
return false
}
试试这个,应该有效:
let items = [2, 6, 4]
var data = [
["id": "1", "title": "Leslie", "color": "brown"],
["id": "8", "title": "Mary", "color": "red"],
["id": "6", "title": "Joe", "color": "blue"],
["id": "2", "title": "Paul", "color": "gray"],
["id": "5", "title": "Stephanie", "color": "pink"],
["id": "9", "title": "Steve", "color": "purple"],
["id": "3", "title": "Doug", "color": "violet"],
["id": "4", "title": "Ken", "color": "white"],
["id": "7", "title": "Annie", "color": "black"]
]
var result = [Dictionary<String, String>]()
for index in 0..<data.count {
let myData = data[index]
let dataID = data[index]["id"]
for i in 0..<items.count {
if dataID == "\(items[i])" {
result.append(myData)
}
}
}
for i in 0..<result.count {
print(result[i])
}
结果是:
谢谢大家的回复!我认为 Eric D 的解决方案最接近我所追求的。
我终于能够在 raywenderlich.com 上找到具有良好描述的解决方案:https://www.raywenderlich.com/82599/swift-functional-programming-tutorial
使用该方法(同样,非常接近 Eric D 的建议),首先我可以将其分解为:
func findItems(value: [String: String]) -> Bool {
return items.contains(Int(value["id"]!)!)
}
var result = data.filter(findItems)
我可以进一步减少到:
var result = data.filter {
(value) in items.contains(Int(value["id"]!)!)
}
同样,可以将其简化为一行,如:
var result = data.filter { items.contains(Int([=12=]["id"]!)!) }
再次感谢大家!