我有一个array
个对象中的一个MyData
(MyData
是一个struct
):
[
MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"),
MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019"),
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019"),
]
我想根据id
键将此数组分组(甚至有意创建一个新数组)。
结果应该是这样的:
[
[MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"), MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019")],
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
[MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019")]
]
即:具有相同ID的数组应形成一个新数组。
当然,我可以简单地循环遍历第一个数组并创建第二个数组,但是我想知道Swift是否可以对其过滤器进行处理。 任何帮助表示赞赏。
答案 0 :(得分:0)
您可以肯定地使用高阶函数,但不能完全使用 100 %来产生所需的结果,但要获得很大的一部分,因为所需的数组类型是:[Any]
。
查看下面的代码:
var myGroup = Dictionary(grouping: arrayOne, by: { $0.id }) // group each element by id -type of: [Int:[Data]]
let resultArray = myGroup.map { $0.value } //map out the elements without the id key. -type of: [[Data]]
//Create hetro Array so we can use it later to append the results
var myHetroArray: [Any] = []
// loop each array in the result array and check if it only contains 1 element if so append that one element to the hetro array otherwise just append the whole thing.
for array in resultArray {
if array.count == 1 {
myHetroArray.append(array.first!)
} else {
myHetroArray.append(array)
}
}
print(myHetroArray) // produce the desired result.
输出: [
[数据(id:19,区域设置:“ de”,标题:“ p1”,日期:“ 11/10/2019”),数据(id:19,区域设置:“ de”,标题:“ p2”,日期: “” 11/10/2019“),数据(id:19,语言环境:” de“,标题:” p3“,日期:” 11/10/2019“))],
数据(id:15,语言环境:“ de”,标题:“ free”,日期:“ 10/11/2019”),
数据(id:32,语言环境:“ fr”,标题:“ free”,日期:“ 10/11/2019”),
[数据(id:3,区域设置:“ en”,标题:“ p1”,日期:“ 10/15/2019”),数据(id:3,区域设置:“ de”,标题:“ p2”,日期: “ 11/12/2019”)]
]
答案 1 :(得分:0)
还有两种方法。就像这里的注释。
struct MyData{
let id : Int
let locale : String
let title : String
let date : String
}
let data = [
MyData(id: 3, locale: "en", title: "p1", date: "10/15/2019"),
MyData(id: 3, locale: "de", title: "p2", date: "11/12/2019"),
MyData(id: 32, locale: "fr", title: "free", date: "10/11/2019"),
MyData(id: 15, locale: "de", title: "free", date: "10/11/2019"),
MyData(id: 19, locale: "de", title: "p1", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p2", date: "11/10/2019"),
MyData(id: 19, locale: "de", title: "p3", date: "11/10/2019"),
]
data.reduce(into: [:]) { ( result, next) in
if result.keys.contains(next.id){
(result[next.id] as? [MyData]).map{result[next.id] = $0 + [next] }
([result[next.id]] as? [MyData]).map{result[next.id] = $0 + [next]}
}
else{ result[next.id] = next }
}.values
Set(data.map{$0.id}).map{id -> Any in
let result = data.filter{$0.id == id}
return result.count == 1 ? result.first! : result
}