arrays - 在 tableview 的 as 部分按日期过滤数组

标签 arrays swift date filter

我有一个这样的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/

相关文章:

swift - 在 Swift 中找不到结构类型的成员 <方法名称>

java - 如果我从不实例化某些子数组,则会为二维数组分配多少空间?

ios - 获取 Laravel JSON feed 中帖子的页码

android - 使用坐标系在android屏幕上绘图

ios - 以编程方式添加的 UITableView 中的错误单元格插入

php - mysql 查询中时间戳的操作符之间

date - SSRS 日期参数输入在英国和美国日期格式之间切换

java - Jodatime 计算 2 个日期范围(几乎是一年)之间的期间的错误

java - java中过滤字符串

c++ - 具有自定义索引的数组