一个对象里的所有空字符串替换成null (递归替换)

1 篇文章 0 订阅

  let  data={
    "title": "导出标题",
    "contentHeader": "一级目录,二级目录,三级目录,题型,题目,分值,是否简要评议",
    "contentList": [
        {
            "category": "语文1",
            "rowNum": 3,
            "children": [
                {
                    "category": "规范性",
                    "rowNum": 2,
                    "children": [
                        {
                            "category": "科学性",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "教学内容正确,无科学性和知识性的错误(0-2,错一处扣1分,扣完为止)",
                            "score": "2",
                            "isBrief": "是",
                            "parentSort": 1,
                            "secondSort": 1,
                            "thirdSort": 1
                        },
                        {
                            "category": "规范性",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "文字、符号、单位和公式符合国家标准(0-3,错一处扣1分,扣完为止)",
                            "score": "3",
                            "isBrief": "",
                            "parentSort": 1,
                            "secondSort": 1,
                            "thirdSort": 2
                        }
                    ],
                    "questionType": "",
                    "topic": "",
                    "score": "",
                    "isBrief": "",
                    "parentSort": "",
                    "secondSort": 1,
                    "thirdSort": ""
                },
                {
                    "category": "资源扩展",
                    "rowNum": 2,
                    "children": [
                        {
                            "category": "资源形式",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "有丰富的和教学内容配合的各种资料、学习辅助材料或网络资源,有利于学生学习(0-5)",
                            "score": "5",
                            "isBrief": "",
                            "parentSort": 1,
                            "secondSort": 3,
                            "thirdSort": 1
                        },
                        {
                            "category": "资源引用",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "引用资源来源清楚,确切可信(0-5)",
                            "score": "5",
                            "isBrief": "是",
                            "parentSort": 1,
                            "secondSort": 3,
                            "thirdSort": 2
                        }
                    ],
                    "questionType": "",
                    "topic": "",
                    "score": "",
                    "isBrief": "",
                    "parentSort": "",
                    "secondSort": 3,
                    "thirdSort": ""
                }
            ],
            "questionType": "",
            "topic": "",
            "score": "",
            "isBrief": "",
            "parentSort": 1,
            "secondSort": "",
            "thirdSort": ""
        },
        {
            "category": "写作听力联系",
            "rowNum": 3,
            "children": [
                {
                    "category": "规范性",
                    "rowNum": 2,
                    "children": [
                        {
                            "category": "科学性",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "教学内容正确,无科学性和知识性的错误(0-2,错一处扣1分,扣完为止)",
                            "score": "",
                            "isBrief": "",
                            "parentSort": 6,
                            "secondSort": 1,
                            "thirdSort": 1
                        },
                        {
                            "category": "规范性",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "文字、符号、单位和公式符合国家标准(0-3,错一处扣1分,扣完为止)",
                            "score": "",
                            "isBrief": "",
                            "parentSort": 6,
                            "secondSort": 1,
                            "thirdSort": 2
                        }
                    ],
                    "questionType": "",
                    "topic": "",
                    "score": "",
                    "isBrief": "",
                    "parentSort": "",
                    "secondSort": 1,
                    "thirdSort": ""
                },
                {
                    "category": "资源扩展",
                    "rowNum": 2,
                    "children": [
                        {
                            "category": "资源形式",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "有丰富的和教学内容配合的各种资料、学习辅助材料或网络资源,有利于学生学习(0-5)",
                            "score": "",
                            "isBrief": "",
                            "parentSort": 6,
                            "secondSort": 3,
                            "thirdSort": 1
                        },
                        {
                            "category": "资源引用",
                            "rowNum": "",
                            "children": "",
                            "questionType": "打分",
                            "topic": "引用资源来源清楚,确切可信(0-5)",
                            "score": "",
                            "isBrief": "",
                            "parentSort": 6,
                            "secondSort": 3,
                            "thirdSort": 2
                        }
                    ],
                    "questionType": "",
                    "topic": "",
                    "score": "",
                    "isBrief": "",
                    "parentSort": "",
                    "secondSort": 3,
                    "thirdSort": ""
                }
            ],
            "questionType": "",
            "topic": "",
            "score": "",
            "isBrief": "",
            "parentSort": 6,
            "secondSort": "",
            "thirdSort": ""
        }
    ]
}



function removeNull(data, defaultStr = null) {
if (typeof data != 'object' || data == null) {
    if ((data == '' || data == "")) {
        return defaultStr;
    } else {
        return data;
    }
}
for (const v in data) {
    if (data[v] == '' || data[v] == "") {
        data[v] = defaultStr;
    }

    if (typeof data[v] == 'object' || typeof data[v] == 'array' ) {
       
        removeNull(data[v])
    }
}
}



removeNull(data)
console.log(data.contentList)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值