I am trying to filter an array of objects on some conditions as:
filtered_shifts = myShifts.filter{$0.regionName == region && $0.cityName == city && $0.idNationality == idn && $0.quantityStaff != 0 && $0.shiftDate > todaydate}
I want to filter it more so if there is a shiftType == "night" and shiftType == "day" for the same shiftDate then added to my filtered shifts array.
if there is shift type = day AND type = night for same date then add it to my filtered array
how can i achieve this?
FOR EXAMPLE
Lets say that
myShifts = {
"id": 50,
"id_region": 1,
"id_city": 2,
"id_nationality": 3,
"id_service": 1,
"shift_date": "2018-06-06 00:00:00",
"shift_type": "day",
"weekday": "wed",
"quantity_staff": 5,
"lead_hours": 3,
"created_at": "2018-05-13 16:27:21",
"updated_at": "2018-05-13 16:27:21",
"deleted_at": null,
"city_name": "Khobar",
"region_name": "Eastren"
},
{
"id": 70,
"id_region": 1,
"id_city": 1,
"id_nationality": 3,
"id_service": 1,
"shift_date": "2018-06-06 00:00:00",
"shift_type": "day",
"weekday": "wed",
"quantity_staff": 12,
"lead_hours": 3,
"created_at": "2018-05-21 14:03:02",
"updated_at": "2018-05-21 14:03:02",
"deleted_at": null,
"city_name": "Dammam",
"region_name": "Eastren"
},
{
"id": 74,
"id_region": 1,
"id_city": 1,
"id_nationality": 3,
"id_service": 1,
"shift_date": "2018-06-06 00:00:00",
"shift_type": "night",
"weekday": "wed",
"quantity_staff": 20,
"lead_hours": 3,
"created_at": "2018-05-21 14:40:56",
"updated_at": "2018-05-21 14:40:56",
"deleted_at": null,
"city_name": "Dammam",
"region_name": "Eastren"
},
{
"id": 4,
"id_region": 1,
"id_city": 1,
"id_nationality": 4,
"id_service": 1,
"shift_date": "2018-05-15 00:00:00",
"shift_type": "night",
"weekday": "tue",
"quantity_staff": 5,
"lead_hours": 2,
"created_at": "2018-04-23 11:46:20",
"updated_at": "2018-05-15 10:33:29",
"deleted_at": null,
"city_name": "Dammam",
"region_name": "Eastren"
}
I want:
filtered_Shifts = {
"id": 70,
"id_region": 1,
"id_city": 1,
"id_nationality": 3,
"id_service": 1,
"shift_date": "2018-06-06 00:00:00",
"shift_type": "day",
"weekday": "wed",
"quantity_staff": 12,
"lead_hours": 3,
"created_at": "2018-05-21 14:03:02",
"updated_at": "2018-05-21 14:03:02",
"deleted_at": null,
"city_name": "Dammam",
"region_name": "Eastren"
},
{
"id": 74,
"id_region": 1,
"id_city": 1,
"id_nationality": 3,
"id_service": 1,
"shift_date": "2018-06-06 00:00:00",
"shift_type": "night",
"weekday": "wed",
"quantity_staff": 20,
"lead_hours": 3,
"created_at": "2018-05-21 14:40:56",
"updated_at": "2018-05-21 14:40:56",
"deleted_at": null,
"city_name": "Dammam",
"region_name": "Eastren"
},
cuz in date 2018-06-06 there is shift_type = night AND day ...
shift_typeother than night and day?