- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
您能告诉我如何从对象中删除所有 null 和空字符串值吗?删除 key 时出现错误。
这是我目前所拥有的,但它不能正常工作:
$.each(sjonObj, function(key, value) {
if(value == "" || value == null) {
delete sjonObj.key;
}
});
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"
}
]
};
$.each(sjonObj, function(key, value) {
if(value == "" || value == null) {
delete sjonObj.key;
}
});
console.log(sjonObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
最佳答案
从字面上看,您正在删除 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>
关于javascript - 如何从对象中删除所有 null 和空字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774231/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!