栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从json对象中删除所有null和空字符串值?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从json对象中删除所有null和空字符串值?

sjonObj.key
实际上是要删除。您需要使用数组访问符号:

delete sjonObj[key];

但是,这也将删除value等于0的地方,因为您没有使用严格的比较。使用

===
来代替:

$.each(sjonObj, function(key, value){    if (value === "" || value === null){        delete sjonObj[key];    }});

但是,这只会使对象浅走。要深入地做,可以使用递归:

(function filter(obj) {    $.each(obj, function(key, value){        if (value === "" || value === null){ delete obj[key];        } else if (Object.prototype.toString.call(value) === '[object Object]') { filter(value);        } else if ($.isArray(value)) { $.each(value, function (k,v) { filter(v); });        }    });})(sjonObj);var sjonObj = {  "executionMode": "SEQUENTIAL",  "coreTEEVersion": "3.3.1.4_RC8",  "testSuiteId": "yyy",  "testSuiteFormatVersion": "1.0.0.0",  "testStatus": "IDLE",  "reportPath": "",  "startTime": 0,  "durationBetweenTestCases": 20,  "endTime": 0,  "lastExecutedTestCaseId": 0,  "repeatCount": 0,  "retryCount": 0,  "fixedTimeSyncSupported": false,  "totalRepeatCount": 0,  "totalRetryCount": 0,  "summaryReportRequired": "true",  "postConditionExecution": "ON_SUCCESS",  "testCaseList": [    {      "executionMode": "SEQUENTIAL",      "commandList": [      ],      "testCaseList": [      ],      "testStatus": "IDLE",      "boundTimeDurationForExecution": 0,      "startTime": 0,      "endTime": 0,      "label": null,      "repeatCount": 0,      "retryCount": 0,      "totalRepeatCount": 0,      "totalRetryCount": 0,      "testCaseId": "a",      "summaryReportRequired": "false",      "postConditionExecution": "ON_SUCCESS"    },    {      "executionMode": "SEQUENTIAL",      "commandList": [      ],      "testCaseList": [        {          "executionMode": "SEQUENTIAL",          "commandList": [ {   "commandParameters": {     "serverAddress": "www.ggp.com",     "echoRequestCount": "",     "sendPacketSize": "",     "interval": "",     "ttl": "",     "addFullDataInReport": "True",     "maxRTT": "",     "failOnTargetHostUnreachable": "True",     "failOnTargetHostUnreachableCount": "",     "initialDelay": "",     "commandTimeout": "",     "testDuration": ""   },   "commandName": "Ping",   "testStatus": "IDLE",   "label": "",   "reportFileName": "tc_2-tc_1-cmd_1_Ping",   "endTime": 0,   "startTime": 0,   "repeatCount": 0,   "retryCount": 0,   "totalRepeatCount": 0,   "totalRetryCount": 0,   "postConditionExecution": "ON_SUCCESS",   "detailReportRequired": "true",   "summaryReportRequired": "true" }          ],          "testCaseList": [          ],          "testStatus": "IDLE",          "boundTimeDurationForExecution": 0,          "startTime": 0,          "endTime": 0,          "label": null,          "repeatCount": 0,          "retryCount": 0,          "totalRepeatCount": 0,          "totalRetryCount": 0,          "testCaseId": "dd",          "summaryReportRequired": "false",          "postConditionExecution": "ON_SUCCESS"        }      ],      "testStatus": "IDLE",      "boundTimeDurationForExecution": 0,      "startTime": 0,      "endTime": 0,      "label": null,      "repeatCount": 0,      "retryCount": 0,      "totalRepeatCount": 0,      "totalRetryCount": 0,      "testCaseId": "b",      "summaryReportRequired": "false",      "postConditionExecution": "ON_SUCCESS"    }  ]};(function filter(obj) {    $.each(obj, function(key, value){        if (value === "" || value === null){ delete obj[key];        } else if (Object.prototype.toString.call(value) === '[object Object]') { filter(value);        } else if (Array.isArray(value)) { value.forEach(function (el) { filter(el); });        }    });})(sjonObj);console.log(sjonObj)<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

请注意,如果您愿意使用lodash /
underscore.js之类的库,则可以

_.pick
改用。但是,由于两个库都没有提供深层过滤功能,因此您仍然需要使用递归进行深层过滤。

sjonObj = (function filter(obj) {    var filtered = _.pick(obj, function (v) { return v !== '' && v !== null; });    return _.cloneDeep(filtered, function (v) { return v !== filtered && _.isPlainObject(v) ? filter(v) : undefined; });})(sjonObj);

此变体具有保留原始对象不变的附加优点,但是它确实创建了一个全新的副本,如果您不需要原始对象,效率将会降低。

var sjonObj = {  "executionMode": "SEQUENTIAL",  "coreTEEVersion": "3.3.1.4_RC8",  "testSuiteId": "yyy",  "testSuiteFormatVersion": "1.0.0.0",  "testStatus": "IDLE",  "reportPath": "",  "startTime": 0,  "durationBetweenTestCases": 20,  "endTime": 0,  "lastExecutedTestCaseId": 0,  "repeatCount": 0,  "retryCount": 0,  "fixedTimeSyncSupported": false,  "totalRepeatCount": 0,  "totalRetryCount": 0,  "summaryReportRequired": "true",  "postConditionExecution": "ON_SUCCESS",  "testCaseList": [    {      "executionMode": "SEQUENTIAL",      "commandList": [      ],      "testCaseList": [      ],      "testStatus": "IDLE",      "boundTimeDurationForExecution": 0,      "startTime": 0,      "endTime": 0,      "label": null,      "repeatCount": 0,      "retryCount": 0,      "totalRepeatCount": 0,      "totalRetryCount": 0,      "testCaseId": "a",      "summaryReportRequired": "false",      "postConditionExecution": "ON_SUCCESS"    },    {      "executionMode": "SEQUENTIAL",      "commandList": [      ],      "testCaseList": [        {          "executionMode": "SEQUENTIAL",          "commandList": [ {   "commandParameters": {     "serverAddress": "www.ggp.com",     "echoRequestCount": "",     "sendPacketSize": "",     "interval": "",     "ttl": "",     "addFullDataInReport": "True",     "maxRTT": "",     "failOnTargetHostUnreachable": "True",     "failOnTargetHostUnreachableCount": "",     "initialDelay": "",     "commandTimeout": "",     "testDuration": ""   },   "commandName": "Ping",   "testStatus": "IDLE",   "label": "",   "reportFileName": "tc_2-tc_1-cmd_1_Ping",   "endTime": 0,   "startTime": 0,   "repeatCount": 0,   "retryCount": 0,   "totalRepeatCount": 0,   "totalRetryCount": 0,   "postConditionExecution": "ON_SUCCESS",   "detailReportRequired": "true",   "summaryReportRequired": "true" }          ],          "testCaseList": [          ],          "testStatus": "IDLE",          "boundTimeDurationForExecution": 0,          "startTime": 0,          "endTime": 0,          "label": null,          "repeatCount": 0,          "retryCount": 0,          "totalRepeatCount": 0,          "totalRetryCount": 0,          "testCaseId": "dd",          "summaryReportRequired": "false",          "postConditionExecution": "ON_SUCCESS"        }      ],      "testStatus": "IDLE",      "boundTimeDurationForExecution": 0,      "startTime": 0,      "endTime": 0,      "label": null,      "repeatCount": 0,      "retryCount": 0,      "totalRepeatCount": 0,      "totalRetryCount": 0,      "testCaseId": "b",      "summaryReportRequired": "false",      "postConditionExecution": "ON_SUCCESS"    }  ]};sjonObj = (function filter(obj) {    var filtered = _.pick(obj, function (v) { return v !== '' && v !== null; });    return _.cloneDeep(filtered, function (v) { return v !== filtered && _.isPlainObject(v) ? filter(v) : undefined; });})(sjonObj);console.log(sjonObj);<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/416128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号