Search code examples
objective-cnsarray

Get the Array list based on the duplicate values in that Array Objective C


i have stuck up with below problem. I have got the below response.

(
 {
     CarrId = 102;
     CarrName = "Fast and Furious";
     CarrOptions =         (
                               {
                                   Id = 8;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 9;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "M-30";
                               },
                               {
                                   Id = 10;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 11;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "M-30";
                               },
                               {
                                   Id = 12;
                                   Img = "603.jpg";
                                   PImg = "611.jpg";
                                   PQ = "L-30";
                               },
                               {
                                   Id = 13;
                                   Img = "738.jpg";
                                   PImg = "749.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 14;
                                   Img = "738.jpg";
                                   PImg = "749.jpg";
                                   PQ = "M-30";
                               }
                               );
     Status = 1;
 }
 )

Get the Array list based on the duplicate values in that Array Objective C. I need to get the array based on the PImg. For example if PImg = 412.jpg. i need the array something lik below

                               {
                                   Id = 8;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "S-30";
                               },
                               {
                                   Id = 9;
                                   Img = "400.jpg";
                                   PImg = "412.jpg";
                                   PQ = "M-30";
                               }

I have only done like getting the duplicates from the response

NSMutableDictionary * thisRow = [resultArray objectAtIndex:0];
NSMutableArray  *pdctDtls = [thisRow objectForKey:@"CarrOptions"];

NSOrderedSet *orderedSet = [[NSOrderedSet orderedSetWithArray:pdctDtls] valueForKey:@"PImg"];
NSMutableArray *newarray = [orderedSet mutableCopy];

In newarray am getting the duplicate values. but if in the newarray if 412.jpg get the array related to 412.jpg. TIA


Solution

  • Try this. If you pass 611.jpg, you get 3 objects.

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", @"611.jpg"]; // AS PER YOUR REQUIREMENT
    NSArray *array =[pdctDtls filteredArrayUsingPredicate:predicate];