js递归json数据时丢失部分json数据

就在今天2018年8月10号  遇到json部分数据丢失很是纳闷:

先来看数据结构:

[{
    "b_id": 1,
    "father_id": 1,
    "children_id": 0,
    "text": "工作平台",
    "time": 1531735386000,
    "name": "AC",
    "statu": 0,
    "btn_id": 0,
    "children": [{
        "b_id": 13,
        "father_id": 57,
        "children_id": 1,
        "text": "客户管理",
        "time": 1531735454000,
        "name": "AC",
        "statu": 0,
        "btn_id": 0,
        "children": [{
            "b_id": 14,
            "father_id": 58,
            "children_id": 57,
            "text": "我的客户",
            "time": 1531735454000,
            "name": "AC",
            "statu": 0,
            "btn_id": 0,
            "children": []
        }, {
            "b_id": 15,
            "father_id": 59,
            "children_id": 57,
            "text": "公海客户",
            "time": 1531735454000,
            "name": "AC",
            "statu": 0,
            "btn_id": 0,
            "children": []
        }]
    }, {
        "b_id": 33,
        "father_id": 66,
        "children_id": 1,
        "text": "测试one",
        "time": 1532416818000,
        "name": null,
        "statu": 0,
        "btn_id": 0,
        "children": [{
            "b_id": 34,
            "father_id": 67,
            "children_id": 66,
            "text": "测试two",
            "time": 1532417008000,
            "name": null,
            "statu": 0,
            "btn_id": 0,
            "children": null
        }]
    }]
}]

仔细看这个数据结构有什么不同处,仔细观察json key为children  有的是数组,有的是null 这就是部分数据丢失的原因

因为我在递归的时候递归是children数组  所以在当遇到children 为null 的时候 undefined 不继续往下执行

附上代码:

function checkBox(data){
   for (var j=0;j<data.length;j++){
     checkBox(data[j].children);
    }
}

解决方案:

 1,判断children是不是数组

2,判断数组是不是null 如果是null 跳出循环,进行下一次循环

当然还有其他的解决办法或者产生数据丢失也不止是因为children是null  供参考;

经过一番努力后,所有东西正慢慢变成我想要的样子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值