0

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 ...

13
  • 2
    Please share your sample array with objects Commented May 30, 2018 at 9:38
  • @PPL just did .. please check it out Commented May 30, 2018 at 9:46
  • Is there any possible value of shift_type other than night and day? Commented May 30, 2018 at 9:50
  • @nayem no ..... Commented May 30, 2018 at 9:51
  • you can first check for night and day condition if true then use you above condition. Commented May 30, 2018 at 9:52

1 Answer 1

0
import Foundation

struct MyShifts: Codable {
    let id :Int
    let id_region: Int
    let id_city: Int
    let id_nationality: Int
    let id_service: Int
    let shift_date: String
    let shift_type: String
    let weekday: String
    let quantity_staff : Int
    let lead_hours: Int
    let created_at: String
    let updated_at: String
    let deleted_at:String
    let city_name: String
    let region_name: String
}



let 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": "2018-05-13 16:27:21",
"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": "2018-05-15 10:33:29",
    "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": "2018-05-15 10:33:29",
    "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": "2018-05-15 10:33:29",
    "city_name": "Dammam",
    "region_name": "Eastren"
}]
""".data(using: .utf8)! // our native (JSON) data

let myShiftsObjectArray = try JSONDecoder().decode([MyShifts].self, from: myShifts) // decoding our data
myShiftsObjectArray.forEach { print($0) } // decoded!
var Dates = Set<String>()
var uniqueArray = [MyShifts]()
for shift in myShiftsObjectArray {
    if !Dates.contains(shift.shift_date) {
        uniqueArray.append(shift)
        Dates.insert(shift.shift_date)
    }
}

print(uniqueArray.count)
var NewFilteredShifts = NSMutableArray()
for loop in uniqueArray
{
    for loopAll in myShiftsObjectArray
    {
        if loop.shift_date == loopAll.shift_date && !NewFilteredShifts.contains(loopAll)
        {
             if loopAll.region_name == "Eastren" && loopAll.city_name == "Dammam" && loopAll.id_nationality == 4 && loopAll.quantity_staff != 0 && (loopAll.shift_type == "day" || loopAll.shift_type == "night")
            {
                NewFilteredShifts.add(loopAll)

            }
        }

    }
}
print(NewFilteredShifts)

I hope This will help you out.

Sign up to request clarification or add additional context in comments.

10 Comments

you have to do some changes according to your need this will work fine :)
there is one mistake i am updating it
Perfect! Thank you so much!
I fixed it as needed and its perfect! :)
My pleasure HappyCoding.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.