js 递归修改json的key键

8 篇文章 0 订阅
4 篇文章 0 订阅

首先看一下我的json数结构:

[{
    "id": 1,
    "text": "工作平台",
    "m_code": "001",
    "m_childrenId": 0,
    "iconCls": "fa fa-television fa-2x",
    "m_url": "ww.baidu.com",
    "m_time": 1531222397000,
    "m_text": "",
    "m_sort": 0,
    "m_level": 0,
    "m_statu": 0,
    "m_adminName": "AC",
    "children": [{
        "id": 57,
        "text": "客户管理",
        "m_code": "",
        "m_childrenId": 1,
        "iconCls": "icon-users",
        "m_url": "暂空",
        "m_time": 1531377348000,
        "m_text": "客户管理",
        "m_sort": 101,
        "m_level": 0,
        "m_statu": 0,
        "m_adminName": "AC",
        "children": [{
            "id": 58,
            "text": "我的客户",
            "m_code": "",
            "m_childrenId": 57,
            "iconCls": "icon-user-tick",
            "m_url": "暂空",
            "m_time": 1531377422000,
            "m_text": "我的客户",
            "m_sort": 103,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [],
            "userid": 2
        }, {
            "id": 59,
            "text": "公海客户",
            "m_code": "",
            "m_childrenId": 57,
            "iconCls": "icon-user-home",
            "m_url": "暂空",
            "m_time": 1531377526000,
            "m_text": "公共客户",
            "m_sort": 103,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [],
            "userid": 2
        }],
        "menuBnt": [],
        "userid": 2
    }, {
        "id": 66,
        "text": "测试one",
        "m_code": "",
        "m_childrenId": 1,
        "iconCls": "icon-arrow-turn-left",
        "m_url": "发斯蒂芬",
        "m_time": 1532416817000,
        "m_text": "个天热",
        "m_sort": 24234213,
        "m_level": 0,
        "m_statu": 0,
        "m_adminName": "AC",
        "children": [{
            "id": 67,
            "text": "测试two",
            "m_code": "",
            "m_childrenId": 66,
            "iconCls": "icon-arrow-divide",
            "m_url": "错位为区分去",
            "m_time": 1532417008000,
            "m_text": "沙发斯蒂芬",
            "m_sort": 54352352,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [{
                "mt_id": 33,
                "menu_id": 67,
                "mt_url": "发士大夫撒的",
                "mt_code": "",
                "text": "测试按钮",
                "mtbtn_type": "",
                "iconCls": "icon-arrow-divide",
                "mt_statu": 0,
                "mt_time": 1532419506000,
                "mt_remark": "发送的",
                "mt_adminName": "AC",
                "mt_sort": 423423,
                "mt_type": 0,
                "mt_operaion": null
            }],
            "userid": 2
        }],
        "menuBnt": [],
        "userid": 2
    }],
    "menuBnt": [],
    "userid": 2
}, {
    "id": 2,
    "text": "工具",
    "m_code": "002",
    "m_childrenId": 0,
    "iconCls": "fa fa-cubes fa-2x",
    "m_url": "ww.baidu.com",
    "m_time": 1531220380000,
    "m_text": "",
    "m_sort": 0,
    "m_level": 0,
    "m_statu": 0,
    "m_adminName": "AC",
    "children": [],
    "menuBnt": [],
    "userid": 2
}, {
    "id": 3,
    "text": "数据统计",
    "m_code": "003",
    "m_childrenId": 0,
    "iconCls": "fa fa-bar-chart fa-2x",
    "m_url": "ww.baidu.com",
    "m_time": 1528427033000,
    "m_text": "",
    "m_sort": 0,
    "m_level": 0,
    "m_statu": 0,
    "m_adminName": null,
    "children": [],
    "menuBnt": [],
    "userid": 2
}, {
    "id": 4,
    "text": "系统管理",
    "m_code": "004",
    "m_childrenId": 0,
    "iconCls": "fa fa-cog fa-2x",
    "m_url": "ww.baidu.com",
    "m_time": 1528427033000,
    "m_text": "",
    "m_sort": 0,
    "m_level": 0,
    "m_statu": 0,
    "m_adminName": null,
    "children": [{
        "id": 6,
        "text": "权限管理",
        "m_code": "",
        "m_childrenId": 4,
        "iconCls": "icon-user-business-boss",
        "m_url": "暂空",
        "m_time": 1531222365000,
        "m_text": "权限管理",
        "m_sort": 401,
        "m_level": 0,
        "m_statu": 0,
        "m_adminName": "AC",
        "children": [{
            "id": 19,
            "text": "用户管理",
            "m_code": "",
            "m_childrenId": 6,
            "iconCls": "icon-user-b",
            "m_url": "暂空",
            "m_time": 1531301436000,
            "m_text": "用户管理",
            "m_sort": 408,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [],
            "userid": 2
        }, {
            "id": 22,
            "text": "角色管理",
            "m_code": "",
            "m_childrenId": 6,
            "iconCls": "icon-user-group",
            "m_url": "/RoleController/getRoleInfoView",
            "m_time": 1531270357000,
            "m_text": "角色管理",
            "m_sort": 404,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [{
                "mt_id": 23,
                "menu_id": 22,
                "mt_url": "addRole()",
                "mt_code": "",
                "text": "新增角色",
                "mtbtn_type": "",
                "iconCls": "icon-add1",
                "mt_statu": 0,
                "mt_time": 1531378272000,
                "mt_remark": "新增角色",
                "mt_adminName": "AC",
                "mt_sort": 1,
                "mt_type": 0,
                "mt_operaion": null
            }, {
                "mt_id": 24,
                "menu_id": 22,
                "mt_url": "updataRole()",
                "mt_code": "",
                "text": "编辑角色",
                "mtbtn_type": "",
                "iconCls": "icon-edit",
                "mt_statu": 0,
                "mt_time": 1531298204000,
                "mt_remark": "角色编辑",
                "mt_adminName": "AC",
                "mt_sort": 2,
                "mt_type": 0,
                "mt_operaion": null
            }, {
                "mt_id": 25,
                "menu_id": 22,
                "mt_url": "deleteRole()",
                "mt_code": "",
                "text": "删除角色",
                "mtbtn_type": "",
                "iconCls": "icon-DeleteRed",
                "mt_statu": 0,
                "mt_time": 1532439217000,
                "mt_remark": "角色删除",
                "mt_adminName": "AC",
                "mt_sort": 3,
                "mt_type": 0,
                "mt_operaion": null
            }, {
                "mt_id": 26,
                "menu_id": 22,
                "mt_url": "updateRole()",
                "mt_code": "",
                "text": "编辑角色权限",
                "mtbtn_type": "",
                "iconCls": "icon-group-edit",
                "mt_statu": 0,
                "mt_time": 1532567168000,
                "mt_remark": "角色权限编辑",
                "mt_adminName": "AC",
                "mt_sort": 4,
                "mt_type": 0,
                "mt_operaion": null
            }, {
                "mt_id": 27,
                "menu_id": 22,
                "mt_url": "暂空",
                "mt_code": "",
                "text": "成员管理",
                "mtbtn_type": "",
                "iconCls": "icon-user",
                "mt_statu": 0,
                "mt_time": 1531273164000,
                "mt_remark": "角色成员管理",
                "mt_adminName": "AC",
                "mt_sort": 5,
                "mt_type": 0,
                "mt_operaion": null
            }, {
                "mt_id": 31,
                "menu_id": 22,
                "mt_url": "refeshRoleList()",
                "mt_code": "",
                "text": "重载",
                "mtbtn_type": "",
                "iconCls": "icon-arrow-refresh",
                "mt_statu": 0,
                "mt_time": 1531297390000,
                "mt_remark": "重载角色列表",
                "mt_adminName": "AC",
                "mt_sort": 12,
                "mt_type": 0,
                "mt_operaion": null
            }],
            "userid": 2
        }, {
            "id": 24,
            "text": "部门管理",
            "m_code": "",
            "m_childrenId": 6,
            "iconCls": "icon-door",
            "m_url": "暂空",
            "m_time": 1530668805000,
            "m_text": "部门管理",
            "m_sort": 406,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [],
            "userid": 2
        }, {
            "id": 25,
            "text": "组织架构",
            "m_code": "",
            "m_childrenId": 6,
            "iconCls": "icon-chart-organisation",
            "m_url": "暂空",
            "m_time": 1530668805000,
            "m_text": "组织架构",
            "m_sort": 402,
            "m_level": 0,
            "m_statu": 0,
            "m_adminName": "AC",
            "children": [],
            "menuBnt": [],
            "userid": 2
        }],
        "menuBnt": [],
        "userid": 2
    }, {
        "id": 60,
        "text": "测试",
        "m_code": "",
        "m_childrenId": 4,
        "iconCls": "icon-award-star-gold-2",
        "m_url": "暂空",
        "m_time": 1531452721000,
        "m_text": "阿萨德",
        "m_sort": 2323,
        "m_level": 0,
        "m_statu": 0,
        "m_adminName": "AC",
        "children": [],
        "menuBnt": [],
        "userid": 2
    }],
    "menuBnt": [],
    "userid": 2
}, {
    "id": 5,
    "text": "更多",
    "m_code": "005",
    "m_childrenId": 0,
    "iconCls": "fa fa-list fa-2x",
    "m_url": "ww.baidu.com",
    "m_time": 1531220589000,
    "m_text": "",
    "m_sort": 0,
    "m_level": 0,
    "m_statu": 0,
    "m_adminName": "AC",
    "children": [],
    "menuBnt": [],
    "userid": 2
}]

要求 修改menuBnt长度大于0的key把menuBnt key 改为children 采用的js 递归处理方式

废话不多讲上代码:

rolemenu 就是json串
for (var i = 0; i < rolemenu.length; i++) {
    //重要部分
    var value=rolemenu[i].children;
    for(var j in rolemenu[i]){
        if(j=='menuBnt' &&  rolemenu[i].menuBnt.length){
            if(rolemenu[i].children.length<=0){
               //把要删除menuBnt的值拿出来赋到新的key中
                rolemenu[i]["children"]=rolemenu[i].menuBnt;
            }
            //删除旧的menuBnt
            delete rolemenu[i].menuBnt;
        }
    }
   //递归 这里递归的时候注意 一定不能写rolemenu[i].children 
    roleCheckBox(value);
}

希望给同学们带来的帮助!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值