Search code examples
iosobjective-cnsarray

How to find the common object from two different arrays and print another object?


I have 2 Arrays receiving from 2 different APIs

booking array

{
            addrLine1 = "Al Thanyah Fifth, Dubai, United Arab Emirates";
            addrLine2 = "  ";
            apntDate = "14 Feb 2018";
            apntDt = "2018-02-14 15:31:30";
            apntTime = "03:31 pm";
            apptLat = "25.071102142334";
            apptLong = "55.142993927002";
            "appt_duration" = "";
            bid = 555;
            bookType = 1;
            cancelAmt = 30;
            "cat_id" = 591da979227951a10f004ad7;
            "cat_name" = "Car Wash";
            cid = 21;
            "coupon_discount" = 0;
            "customer_notes" = "";
            "distance_met" = 120;
            email = "fawasfais@gmail.com";
            expireTime = "";
            fname = Fawas;
            "job_imgs" = 0;
            "job_start_time" = "1970-01-01 00:00:00";
            "job_timer" = "";
            pPic = "https://s3.amazonaws.com/iserve/ProfileImages/20170720110011AM.png";
            "payment_type" = 1;
            phone = 568573570;
            "price_per_min" = 10;
            "pro_notes" = "";
            services = "5923f21c2279518a1661cb09,5923f22d2279513f1861cb09";
            status = 2;
            statusMsg = "Provider Accepted.";
            timer = "";
            "timer_status" = "";
            "visit_amount" = 60;
        }  

selected services array

{
        "price_per_unit" = 30;
        "sub_cat_id" = 5923f21c2279518a1661cb09;
        "sub_cat_name" = SUV;
        unit = 1;
    },
        {
        "price_per_unit" = 45;
        "sub_cat_id" = 5923f22d2279513f1861cb09;
        "sub_cat_name" = Sedan;
        unit = 1;
    }
 {
        "price_per_unit" = 65;
        "sub_cat_id" = 5923f23e2279518a1661cb09;
        "sub_cat_name" = Bus;
        unit = 1;
    },
        {
        "price_per_unit" = 75;
        "sub_cat_id" = 5923f24f2279513f1861cb09;
        "sub_cat_name" = Lorry;
        unit = 1;
    }

I want to compare both the above arrays with "services" key from first array and "sub_cat_id" from second array and finally print the price_per_unit of the common service id containing vehicles.

Example - 5923f21c2279518a1661cb09,5923f22d2279513f1861cb09 ids found in the first array so print the price_per_units of the both ids from second array - 30, 45

Any Suggestions ? (If any code snippets requires to answer this question mention in the comments I will edit and update the question with the required codes.

This is how I get the second array from the server

-(void)getServiceTypes
{
    NSDictionary *queryParams;
    queryParams = @{
                    @"ent_sess_token":flStrForStr([[NSUserDefaults standardUserDefaults] objectForKey:KDAcheckUserSessionToken]),
                    @"ent_dev_id":flStrForStr([[NSUserDefaults standardUserDefaults] objectForKey:kPMDDeviceIdKey]),
                   @"ent_catid":_dictAppointmentDetails[@"cat_id"]
                   };


    NetworkHandler *handler = [NetworkHandler sharedInstance];
    [handler composeRequestWithMethod:@"getSubCategory"
                              paramas:queryParams
                         onComplition:^(BOOL succeeded, NSDictionary *response) {
                             if (succeeded) {

                                 [self getServiceTypesResponse:response];
                             }
                         }];
    NSLog(@"param%@",queryParams);

}
-(void)getServiceTypesResponse:(NSDictionary *)response
{
    NSLog(@"GETSERVICETYPES%@",response);

    serviceTypeDict = response[@"data"];
    NSLog(@"SERVICETYPEDIC%@",serviceTypeDict);
    serviceTypeArray = response[@"data"];
    NSLog(@"SERVICETYPEARRAY%@",serviceTypeArray);
    serviceTypeIDArray = [[serviceTypeArray valueForKey:@"sub_cat_id"]copy];
    NSLog(@"SERVICETYPEIDARRAY%@",serviceTypeIDArray);



}

Thanks


Solution

  • Got it working in playground:

    let booking: [String: Any] = [
        "services": "5923f21c2279518a1661cb09,5923f22d2279513f1861cb09",
        "status":2
    ]
    
    let selectedServices: [[String: Any]] = [
        [
            "price_per_unit": 30,
            "sub_cat_id": "5923f21c2279518a1661cb09",
            "sub_cat_name": "SUV",
            "unit": 1
        ],
        [
            "price_per_unit": 45,
            "sub_cat_id": "5923f22d2279513f1861cb09",
            "sub_cat_name": "Sedan",
            "unit": 1
        ],
        [
            "price_per_unit": 65,
            "sub_cat_id": "5923f23e2279518a1661cb09",
            "sub_cat_name": "Bus",
            "unit": 1
        ],
        [
            "price_per_unit": 75,
            "sub_cat_id": "5923f24f2279513f1861cb09",
            "sub_cat_name": "Lorry",
            "unit": 1
        ]
    ]
    
    if let bookingServices = booking["services"] as? String {
        let services: [String] = bookingServices.components(separatedBy: ",")
    
        let commonServices = selectedServices.filter({ services.contains( $0["sub_cat_id"] as? String ?? "" ) })
        print(commonServices)
    }
    

    Objective-C Equivalent:

    NSDictionary *booking = @{
                                    @"services": @"5923f21c2279518a1661cb09,5923f22d2279513f1861cb09",
                                    @"status": @2
                                    };
    
    NSArray *selectedServices =  @[
                 @{
                  @"price_per_unit": @30,
                  @"sub_cat_id": @"5923f21c2279518a1661cb09",
                  @"sub_cat_name": @"SUV",
                  @"unit": @1
                  },
                 @{
                  @"price_per_unit": @45,
                  @"sub_cat_id": @"5923f22d2279513f1861cb09",
                  @"sub_cat_name": @"Sedan",
                  @"unit": @1
                  },
                 @{
                  @"price_per_unit": @65,
                  @"sub_cat_id": @"5923f23e2279518a1661cb09",
                  @"sub_cat_name": @"Bus",
                  @"unit": @1
                  },
                 @{
                  @"price_per_unit": @75,
                  @"sub_cat_id": @"5923f24f2279513f1861cb09",
                  @"sub_cat_name": @"Lorry",
                  @"unit": @1
                  }
                 ];
    
    NSString *servicesString = [booking valueForKey: @"services"];
    NSArray *bookingServices = [servicesString componentsSeparatedByString:@","];
    
    NSMutableArray *commonServices = [[NSMutableArray alloc] init];
    
    for (NSDictionary *selectedService in selectedServices) {
        NSString *sub_cat_id = selectedService[@"sub_cat_id"];
    
        if([bookingServices containsObject: sub_cat_id]) {
            [commonServices addObject: selectedService];
        }
    }
    
    NSLog(@"%@", commonServices);