Home > OS >  Form a new array based on the Id in the array - Objective-c
Form a new array based on the Id in the array - Objective-c

Time:04-26

I have an array like this

{
  OptionId = 5;
  Choice Name = "Tomato" 

},
{
 OptionId = 1;
 Choice Name = "Olives"
},
{
  OptionId = 5;
  ChoiceName = "Mushrooms"
},
{
   OptionId = 6;
   ChoiceName = "BBQ"
}

I want to sort this array so that the result would contain 3 elements

{
 OptionId = 5
 Choices = (
   {
     ChoiceName = "Tomato"
   },
   {
     ChoiceName = "Mushrooms"
   }
)
},
{
 OptionId = 1;
 Choices = (
 {
 Choice Name = "Olives"
)
},
{
 OptionId = 6;
 Choices = (
 {
 Choice Name = "BBQ"
)
}
}

Unable to get where should I start with. Any ideas/suggestions would be heplful

CodePudding user response:

You can do it like that:

NSArray *initialArray = @[@{@"OptionId":@5,
                            @"Choice Name":@"Tomato"},
                          @{@"OptionId":@1,
                            @"Choice Name":@"Olives"
                          },
                          @{@"OptionId":@5,
                            @"Choice Name":@"Mushrooms"
                          },
                          @{@"OptionId":@6,
                            @"Choice Name":@"BBQ"
                          }];

NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (NSDictionary *aDictionary in initialArray) {
    //We find if we already have an entry for that OptionId in the final array
    NSUInteger existingIndex = [finalArray indexOfObjectPassingTest:^BOOL(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        return [obj[@"OptionId"] isEqualTo:aDictionary[@"OptionId"]];
    }];
    if (existingIndex != NSNotFound) {
        // If we found one, we retrieve the element, and append the choice to our existing NSMutableArray
        NSMutableDictionary *dict = finalArray[existingIndex];
        NSMutableArray *existingArray = dict[@"Choice Name"];
        [existingArray addObject:@{@"Choice Name": aDictionary[@"Choice Name"]}];
    } else {
        //Else we create a new entry, and we make the Choice Name an NSMutableArray, since it might have new choices afterwards
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        dict[@"OptionId"] = aDictionary[@"OptionId"];
        dict[@"Choice Name"] = [NSMutableArray arrayWithObject:@{@"Choice Name": aDictionary[@"Choice Name"]}];
        [finalArray addObject:dict];
    }
}
NSLog(@"Initial:\n%@", initialArray);
NSLog(@"Final:\n%@", finalArray);

Output:

$>Initial:
(
        {
        "Choice Name" = Tomato;
        OptionId = 5;
    },
        {
        "Choice Name" = Olives;
        OptionId = 1;
    },
        {
        "Choice Name" = Mushrooms;
        OptionId = 5;
    },
        {
        "Choice Name" = BBQ;
        OptionId = 6;
    }
)
$>Final:
(
        {
        "Choice Name" =         (
                        {
                "Choice Name" = Tomato;
            },
                        {
                "Choice Name" = Mushrooms;
            }
        );
        OptionId = 5;
    },
        {
        "Choice Name" =         (
                        {
                "Choice Name" = Olives;
            }
        );
        OptionId = 1;
    },
        {
        "Choice Name" =         (
                        {
                "Choice Name" = BBQ;
            }
        );
        OptionId = 6;
    }
)

Side note: If possible, use custom NSObject, it would be much better and easier (especially since a typo on a key can break everything).

  • Related