.m file
NSArray *array = @[@{@"groupId" : @"1", @"name" : @"matt"},
@{@"groupId" : @"2", @"name" : @"john"},
@{@"groupId" : @"3", @"name" : @"steve"},
@{@"groupId" : @"4", @"name" : @"alice"},
@{@"groupId" : @"1", @"name" : @"bill"},
@{@"groupId" : @"2", @"name" : @"bob"},
@{@"groupId" : @"3", @"name" : @"jack"},
@{@"groupId" : @"4", @"name" : @"dan"},
@{@"groupId" : @"1", @"name" : @"kevin"},
@{@"groupId" : @"2", @"name" : @"mike"},
@{@"groupId" : @"3", @"name" : @"daniel"},
];
NSMutableArray *resultArray = [NSMutableArray new];
NSArray *groups = [array valueForKeyPath:@"@distinctUnionOfObjects.groupId"];
for (NSString *groupId in groups)
{
NSMutableDictionary *entry = [NSMutableDictionary new];
[entry setObject:groupId forKey:@"groupId"];
NSArray *groupNames = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"groupId = %@", groupId]];
for (int i = 0; i < groupNames.count; i++)
{
NSString *name = [[groupNames objectAtIndex:i] objectForKey:@"name"];
[entry setObject:name forKey:[NSString stringWithFormat:@"name%d", i + 1]];
}
[resultArray addObject:entry];
}
No comments:
Post a Comment