NSArray *array2 = @[
@{@"id" : @"DDS434313", @"name": @"可乐", @"num" : @"5", @"inventory" : @"400"},
@{@"id" : @"OOYU23423", @"name": @"雪碧", @"num" : @"9", @"inventory" : @"300"},
@{@"id" : @"PW0973434", @"name": @"咖啡", @"num" : @"1", @"inventory" : @"80"},
@{@"id" : @"9JLHG2323", @"name": @"橙汁", @"num" : @"11", @"inventory" : @"200"},
@{@"id" : @"JLJLYiiiiOYO1", @"name": @"王老吉", @"num" : @"7", @"inventory" : @"150"},
@{@"id" : @"JLJLYiiiiOYO1", @"name": @"王老吉", @"num" : @"7", @"inventory" : @"150"},
@{@"id" : @"DDS434313", @"name": @"可乐", @"num" : @"7", @"inventory" : @"320"}
];
//会把id一样的商品放在同一个数组里面,只有一个对象,商品订单数量会相加
//SELF.id id就是数据模型中的id
NSMutableArray *dateMutablearray1 = [[NSMutableArray alloc] init];
NSMutableArray *array1 = [NSMutableArray arrayWithArray:array2];
for (int i = 0; i < array1.count; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:array1[i]];
NSString *string = [dic objectForKey:@"id"];
NSString *predicateFormat = [NSString stringWithFormat:@"SELF.id contains[c] '%@'",string];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:predicateFormat];
NSArray *newArray = [array1 filteredArrayUsingPredicate:sPredicate];
if (newArray.count > 1) {
NSInteger count = 0;
for (int index = 0; index < newArray.count; index ++) {
NSDictionary *tempDict = [newArray objectAtIndex:index];
NSString *numStr = [tempDict objectForKey:@"num"];
count += [numStr integerValue];
}
[dic setObject:[NSString stringWithFormat:@"%ld",count] forKey:@"num"];
}
[dateMutablearray1 addObject:dic];
[array1 removeObjectsInArray:newArray];
i --;
}
NSLog(@"%@",dateMutablearray1);
//会把id一样的商品放到同一个数组里面,但是一个数组里面有多个对象,商品订单数量不会相加
//使用NSPredicate实现
NSMutableArray *dateMutablearray = [[NSMutableArray alloc] init];
NSMutableArray *forarray = [NSMutableArray arrayWithArray:array2];
for (int i = 0; i < forarray.count; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:forarray[i]];
NSString *string = [dic objectForKey:@"id"];
NSString *predicateFormat = [NSString stringWithFormat:@"SELF.id contains[c] '%@'",string];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:predicateFormat];
NSArray *newArray = [forarray filteredArrayUsingPredicate:sPredicate];
[dateMutablearray addObject:newArray];
[forarray removeObjectsInArray:newArray];
i --;
}
NSLog(@"%@",dateMutablearray);
//会把id一样的商品放到同一个数组里面,但是一个数组里面有多个对象,商品订单数量不会相加
//使用for循环实现数组
NSMutableArray *dateMutablearray3 = [[NSMutableArray alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithArray:array2];
for (int i = 0; i < array.count; i ++) {
NSDictionary *dic = array[i];
NSString *string = [dic objectForKey:@"id"];
NSMutableArray *tempArray = [@[] mutableCopy];
[tempArray addObject:dic];
for (int j = i+1; j < array.count; j ++) {
NSDictionary *dicc = array[j];
NSString *stringg = [dicc objectForKey:@"id"];
NSString *jstring = stringg;
if([string isEqualToString:jstring]){
[tempArray addObject:dicc];
}
}
if ([tempArray count] >= 1) {
[dateMutablearray3 addObject:tempArray];
[array removeObjectsInArray:tempArray];
i -= 1; //去除重复数据 新数组开始遍历位置不变
}
}
NSLog(@"%@",dateMutablearray3);