根据数组中的Id组成一个新数组 - Objective-c

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

我有一个这样的数组

{
  OptionId = 5;
  Choice Name = "Tomato" 

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

我想对这个数组进行排序,使结果包含 3 个元素

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

无法找到我应该从哪里开始。 任何 ideas/suggestions 都会非常有用

你可以这样做:

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);

输出:

$>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;
    }
)

旁注:如果可能,使用自定义 NSObject,这会更好更容易(特别是因为按键上的错字会破坏一切)。