数组的元素为字典,字典中多条id相同的元素合并为一个元素,并修改name值
for(int i = 0; i < rackSeatMutAry.count; i++)
{
NSMutableDictionary *oldDic = [NSMutableDictionary dictionaryWithDictionary:rackSeatMutAry[i]];
NSString *oldIdStr = [NSString stringWithFormat:@"%@", oldDic[@"id"]];
for(int j = i + 1; j < rackSeatMutAry.count; j++)
{
NSDictionary *newDic = rackSeatMutAry[j];
NSString *newIdStr = [NSString stringWithFormat:@"%@", newDic[@"id"]];
NSString *locationEndStr = [NSString stringWithFormat:@"%@", newDic[@"locationEnd"]];
if([oldIdStr isEqualToString:newIdStr])
{
//拼接字符串
NSString *rackNameStr = [NSString stringWithFormat:@"%@", oldDic[@"rackName"]];
NSString *strUrl = [rackNameStr stringByReplacingOccurrencesOfString:@")" withString:@""]; // 去掉右括号
NSString *rackSeatNameStr = [NSString stringWithFormat:@"%@-%@)", strUrl, locationEndStr];
//重新设置rackSeatName值
[oldDic setValue:rackSeatNameStr forKey:@"rackSeatName"];
[rackSeatMutAry setObject:oldDic atIndexedSubscript:i];
[rackSeatMutAry removeObjectAtIndex:j];
j -= 1;
}
}
}
NSLog(@"newArray = %@\n", rackSeatMutAry);