问题:
将以下数据根据“tableType”值分组处理。
数据如下:
{
"tableType":"6",
"storeNo":"000",
"areaNo":"1",
"areaName":"一组",
"statusName":"占用",
"orderList":[],
"statusNo":"1",
"tableName":"1号桌",
"tableNo":"1"
},
{
"tableType":"4",
"storeNo":"0000",
"areaNo":"1",
"areaName":"一组",
"statusName":"空闲",
"orderList":[],
"statusNo":"0",
"tableName":"27号桌",
"tableNo":"27"
},
{
"tableType":"6",
"storeNo":"000",
"areaNo":"1",
"areaName":"一组",
"statusName":"空闲",
"orderList":[],
"statusNo":"0",
"tableName":"31号桌",
"tableNo":"31"
},
PS:“tableType”值不定,依据后台返回数据为定。
最后分组结果如下:
image.png关键代码如下:
- (void)arrayGroup: (NSArray *)dataArray {
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
if(dataArray) {
//遍历
[dataArray enumerateObjectsUsingBlock:^(TableStatusModel* model, NSUInteger idx, BOOL * _Nonnull stop) {
NSMutableArray *array = dic[model.tableType];
if(!array)
//如果数组不存在创建
array = @[model].mutableCopy;
else
//如果存在添加元素
[array addObject:model];
//根据不同的key,创建对应的字典
[dic setObject:array forKey:model.tableType];
}];
self.tableTypeDic = dic;
}
}
网友评论