Flutter 递归查询Map(字典)

本文介绍了一个使用Dart语言实现的递归算法,该算法能够从一个模拟的公司组织架构数据集中找出所有层级的员工信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 下面代码从模拟的公司组织架构数据中,把各级部们的员工都查找出来。

void main() {
  doFind();
}

void doFind(){
  String jsonString = getJsonString();

  Map<String,dynamic> rootMap = jsonDecode(jsonString);
  
  List foundUsers = [];
  recursiveFind(rootMap,foundUsers);

  print('found user count:${foundUsers.length}');
}

void recursiveFind(Map<String,dynamic> map,List foundUsers){
  List users = map['users'];
  if(users != null){
    foundUsers.addAll(users);
  }

  List organizations = map['organizations'];
  if(organizations == null){
    return;
  }
  for(Map child in organizations){
    //此处递归
    recursiveFind(child,foundUsers);
  }
}

String getJsonString(){
  return  '''
  {
    "id": 6636142779154173952,
    "name": "北京世纪教育科技有限公司",
    "parentId": -1,
    "parentName": null,
    "organizations": [
      {
        "id": 6636240725304545280,
        "name": "战略合作中心",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": null,
        "users": [
          {
            "id": 6637575629174870016,
            "name": "张大继"
          },
          {
            "id": 6637293257711816704,
            "name": "金大瑞"
          }
        ]
      },
      {
        "id": 6636522476308402176,
        "name": "市场中心",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": [
          {
            "id": 6769480335790051328,
            "name": "环京战区",
            "parentId": 6636522476308402176,
            "parentName": "市场中心",
            "organizations": [
              {
                "id": 6637566159619231744,
                "name": "大连分公司",
                "parentId": 6769480335790051328,
                "parentName": "环京战区",
                "organizations": [
                  {
                    "id": 6829963307508699136,
                    "name": "总监一部",
                    "parentId": 6637566159619231744,
                    "parentName": "大连分公司",
                    "organizations": [
                      {
                        "id": 6829999798028996608,
                        "name": "经理一部",
                        "parentId": 6829963307508699136,
                        "parentName": "总监一部",
                        "organizations": null,
                        "users": [
                          {
                            "id": 6776071027786125312,
                            "name": "陈大阳"
                          },
                          {
                            "id": 6677831655157272576,
                            "name": "吴大煜"
                          }
                        ]
                      }
                    ],
                    "users": [
                      {
                        "id": 6853959974348001280,
                        "name": "崔大伟"
                      },
                      {
                        "id": 6776071292794834944,
                        "name": "吴大涵"
                      },
                      {
                        "id": 6776071223655927808,
                        "name": "池大国"
                      }
                    ]
                  }
                ],
                "users": [
                  {
                    "id": 6785359176228016128,
                    "name": "白云"
                  }
                ]
              }
            ],
            "users": null
          }
        ],
        "users": null
      },
      {
        "id": 6637251187156586496,
        "name": "财务部",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": [
          {
            "id": 6769483913367785472,
            "name": "总部财务",
            "parentId": 6637251187156586496,
            "parentName": "财务部",
            "organizations": null,
            "users": [
              {
                "id": 6637325183000645632,
                "name": "程大楠"
              },
              {
                "id": 6637320375967551488,
                "name": "王大连"
              }
            ]
          }
        ],
        "users": null
      },
      {
        "id": 6637251450378522624,
        "name": "总裁办",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": null,
        "users": [
          {
            "id": 6637247835978993664,
            "name": "李大佳"
          },
          {
            "id": 6637214779612205056,
            "name": "李大东"
          }
        ]
      },
      {
        "id": 6636522498269777920,
        "name": "技术中心",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": [
          {
            "id": 6637620686112296960,
            "name": "总部技术中心",
            "parentId": 6636522498269777920,
            "parentName": "技术中心",
            "organizations": null,
            "users": [
              {
                "id": 6840561320513900544,
                "name": "方大勇"
              },
              {
                "id": 6637556098842890240,
                "name": "李大霏"
              }
            ]
          },
          {
            "id": 6637620745000325120,
            "name": "北京大区",
            "parentId": 6636522498269777920,
            "parentName": "技术中心",
            "organizations": null,
            "users": [
              {
                "id": 6637288574620405760,
                "name": "周大媛"
              }
            ]
          },
          {
            "id": 6637620810527936512,
            "name": "东北大区",
            "parentId": 6636522498269777920,
            "parentName": "技术中心",
            "organizations": null,
            "users": [
              {
                "id": 6637292718395625472,
                "name": "池大霁"
              },
              {
                "id": 6722713638320345088,
                "name": "曹大强"
              }
            ]
          },
          {
            "id": 6637620880916746240,
            "name": "山东大区",
            "parentId": 6636522498269777920,
            "parentName": "技术中心",
            "organizations": null,
            "users": [
              {
                "id": 6637293160861143040,
                "name": "伊大帅"
              },
              {
                "id": 6637266416984788992,
                "name": "曾大成"
              },
              {
                "id": 6637626476143448064,
                "name": "迟一礼"
              }
            ]
          }
        ],
        "users": null
      },
      {
        "id": 6637251507035181056,
        "name": "人力资源中心",
        "parentId": 6636142779154173952,
        "parentName": "北京世纪教育科技有限公司",
        "organizations": null,
        "users": [
          {
            "id": 6695878785629294592,
            "name": "闫大京"
          },
          {
            "id": 6709718214345101312,
            "name": "梁大芬"
          }
        ]
      }
    ],
    "users": [
      {
        "id": 6868841707459448832,
        "name": "张大丹"
      },
      {
        "id": 6864502026701770752,
        "name": "蒋大辉"
      }
    ]
  }  
  ''';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值