使用另一个数组中的值过滤数组

时间:2017-10-17 05:50:08

标签: ios swift xcode

我需要使用Schedules[ ] Dates[ ] Array来过滤"date_id" == "schedule_date_id"数组。

我只需要date_id匹配schedule_date_id的元素,然后重新加载表格。我需要根据日期显示时间表。

这是什么Swift代码?

以下是我的JSON:

{
dates =     (
            {
        "date_date" = "2017-11-04";
        "date_id" = 4;
        "date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_1.png";
        "date_title" = "Day 1";
    },
            {
        "date_date" = "2017-11-05";
        "date_id" = 5;
        "date_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedule-days/day_2.png";
        "date_title" = "Day 2";
    }
);
schedules =     (
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 7";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 7;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 8";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 8;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-04";
        "schedule_date_id" = 4;
        "schedule_description" = "Schedule 9";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 9;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_1.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    },
            {
        "schedule_date" = "2017-11-05";
        "schedule_date_id" = 5;
        "schedule_description" = "Schedule 10";
        "schedule_end_time" = "14:00:00";
        "schedule_id" = 10;
        "schedule_image" = "https://project-isdental-cammy92.c9users.io/api/images/schedules/day_2.png";
        "schedule_location" = Mumbai;
        "schedule_start_time" = "11:00:00";
    }
);
}

3 个答案:

答案 0 :(得分:2)

像这样使用:

Swift 4

let filteredSchedules = schedules.filter { schedule in
    dates.contains(where: { $0["date_id"] as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)

Swift 3

let filteredSchedules = schedules.filter { schedule in
    dates.contains({ $0["date_id"] as! Int == schedule["schedule_id"] as! Int })
}
print(filteredSchedules)

答案 1 :(得分:1)

使用来自datesArray

schedules过滤date_id数组
let dateId = 4
let filteredSchedules = schedules.filter { schedule in
    dates.contains(where: { dateId as! Int == schedule["schedule_id"] as! Int }
)}
print(filteredSchedules)

将日期指定给collectionView单元格

cell.titleLabel.text = datesArray[indexPath.row]["datesArray"]

答案 2 :(得分:0)

尝试使用以下代码

let newArray  = schedulesArray.filter { (schedulesDic) -> Bool in
            return datesArray.contains(where: { (datesDic) -> Bool in
                if schedulesDic["schedule_id"] as! Int == datesDic["date_id"] as! Int{
                    return true
                }
                else {
                    return false
                }
            })
        }
print(newArray)

或以上代码的简短形式

let newArray  = schedulesArray.filter { (schedulesDic) -> Bool in
            datesArray.contains(where: { $0["date_id"] as! Int == schedulesDic["schedule_id"] as! Int })
        }
print(newArray)