我有一个这样的json
"myAlert": [{
"usa_time": "2018-04-27 10:42:45",
"vs_title": "Kenya ",
"vs_start_time": "2018-04-29 18:00:00",
"vs_end_time": "2018-04-29 18:50:00",
"vs_status": "1",
"vs_add_time": "2018-04-29 00:00:00"
}, {
"usa_time": "2018-04-27 10:42:37",
"vs_title": "Association",
"vs_start_time": "2018-04-29 14:45:00",
"vs_end_time": "2018-04-29 15:30:00",
"vs_status": "1",
"vs_add_time": "2018-04-29 00:00:00"
}, {
"usa_time": "2018-04-27 10:42:27",
"vs_title": "Negotiations ",
"vs_start_time": "2018-04-28 19:00:00",
"vs_end_time": "2018-04-28 19:45:00",
"vs_status": "1",
"vs_add_time": "2018-04-28 00:00:00"
}, {
"usa_time": "2018-04-27 10:42:27",
"vs_title": "Negotiations ",
"vs_start_time": "2018-04-28 19:00:00",
"vs_end_time": "2018-04-28 19:45:00",
"vs_status": "1",
"vs_add_time": "2018-04-28 00:00:00"
}]
我想将此数据过滤到 vs_add_time
中。如果数据具有相似的 vs_add_time
,则日期将成为部分,并且具有相同日期的数据将作为字典合并到日期数组中。
我想将数据过滤成这种格式:
"myAlert": [
'2018-04-29': [{
"usa_time": "2018-04-27 10:42:45",
"vs_title": "Kenya ",
"vs_start_time": "2018-04-29 18:00:00",
"vs_end_time": "2018-04-29 18:50:00",
"vs_status": "1",
"vs_add_time": "2018-04-29 00:00:00"
}, {
"usa_time": "2018-04-27 10:42:41",
"vs_title": "Kiyoshi El ",
"vs_start_time": "2018-04-29 16:00:00",
"vs_end_time": "2018-04-29 17:30:00",
"vs_status": "1",
"vs_add_time": "2018-04-29 00:00:00"
}, ],
'2018-04-28': [{
"usa_time": "2018-04-27 10:42:27",
"vs_title": "Negotiations ",
"vs_start_time": "2018-04-28 19:00:00",
"vs_end_time": "2018-04-28 19:45:00",
"vs_status": "1",
"vs_add_time": "2018-04-28 00:00:00"
}, {
"usa_time": "2018-04-27 10:42:27",
"vs_title": "Negotiations ",
"vs_start_time": "2018-04-28 19:00:00",
"vs_end_time": "2018-04-28 19:45:00",
"vs_status": "1",
"vs_add_time": "2018-04-28 00:00:00"
}]
]
任何人都可以让我走上正轨吗?
提前致谢
最佳答案
你可以使用 Dictionary(grouping:by:)
作为:
let groupedDict = Dictionary(grouping: myAlert, by: { $0["vs_add_time"]! })
字典(分组:按:)
Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
The arrays in the "values" position of the new dictionary each contain at least one element, with the elements in the same order as the source sequence.
关于arrays - 在 tableview 的 as 部分按日期过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50219440/