来自字典数组的字典 - Objective-C 中的逻辑挑战
Dictionary from an Array of Dictionary - Logical Challenge in Objective-C
目标是将字典数组转换为一个包含不同对象的字典。我该怎么做?
我有一个这种格式的记录数组,
originalArray = [{
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
}
]
我想创建以下格式的字典。
{
"032019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
"CODE2" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
"status": "Filed"
}
},
"042019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
}
}
}
这是我的示例,你可以自己查看
NSArray *array = @[
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"17-05-2019",@"rtntype":@"CODE1",@"ret_prd":@"042019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE1",@"ret_prd":@"032019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE2",@"ret_prd":@"032019"}
];
NSMutableDictionary *result = [NSMutableDictionary new];
for (int i = 0; i < array.count; i++) {
NSDictionary *dict = array[i];
NSString *value = dict[@"ret_prd"];
if (result[value] == nil) {
NSArray *filterArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(ret_prd contains[c] %@)", value]];
NSMutableDictionary *filterDict = [NSMutableDictionary new];
for (int i = 0; i < filterArray.count; i++) {
filterDict[filterArray[i][@"rtntype"]] = filterArray[i];
}
result[value] = filterDict;
}
}
NSLog(@"%@", result);
控制台日志中的结果:
好久没有代码Objective-C,所以上面的代码可能很奇怪。有不懂的可以私信我,我给你解释。
目标是将字典数组转换为一个包含不同对象的字典。我该怎么做?
我有一个这种格式的记录数组,
originalArray = [{
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
{
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
}
]
我想创建以下格式的字典。
{
"032019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE1",
"ret_prd": "032019",
},
"CODE2" = {
"valid": "Y",
"mof": "ON",
"dof": "19-04-2019",
"rtntype": "CODE2",
"ret_prd": "032019",
"status": "Filed"
}
},
"042019" = {
"CODE1" = {
"valid": "Y",
"mof": "ON",
"dof": "17-05-2019",
"rtntype": "CODE1",
"ret_prd": "042019",
}
}
}
这是我的示例,你可以自己查看
NSArray *array = @[
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"17-05-2019",@"rtntype":@"CODE1",@"ret_prd":@"042019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE1",@"ret_prd":@"032019"},
@{@"valid":@"Y",@"mof":@"ON",@"dof":@"19-04-2019",@"rtntype":@"CODE2",@"ret_prd":@"032019"}
];
NSMutableDictionary *result = [NSMutableDictionary new];
for (int i = 0; i < array.count; i++) {
NSDictionary *dict = array[i];
NSString *value = dict[@"ret_prd"];
if (result[value] == nil) {
NSArray *filterArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(ret_prd contains[c] %@)", value]];
NSMutableDictionary *filterDict = [NSMutableDictionary new];
for (int i = 0; i < filterArray.count; i++) {
filterDict[filterArray[i][@"rtntype"]] = filterArray[i];
}
result[value] = filterDict;
}
}
NSLog(@"%@", result);
控制台日志中的结果:
好久没有代码Objective-C,所以上面的代码可能很奇怪。有不懂的可以私信我,我给你解释。