开发者社区> 问答> 正文

词典数组中的词典--内部监督办公室专家面临的逻辑挑战--“目标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",
                }
            }
        }

展开
收起
游客5akardh5cojhg 2019-12-15 16:46:47 423 0
1 条回答
写回答
取消 提交回答
  • 这是我的样品,你可以自己检查

    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);
    
    

    控制台日志的结果:

    https://i.stack.imgur.com/WPEBH.png

    很长时间没有代码目标-C,所以上面的代码可能很奇怪。如果你不知道在哪里,我会给你解释的。

    2019-12-15 16:47:57
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
千里之外,洞悉风险 立即下载
AI在双11中的个性化搜索和决策实践 立即下载
让机器逐渐习得法律认知能力-在数据孤岛间搭建连接的道路 立即下载