To remove all empty string values from an object:Use the Object. keys() method to get an array of the object's keys. Use the forEach() method to iterate over the array. Use the delete operator to delete each key with an empty string value.
To remove all null values from an array:Declare a results variable and set it to an empty array. Use the forEach() method to iterate over the array. Check if each element is not equal to null . If the condition is satisfied, push the element into the results array.
The special JavaScript keyword delete is used to remove object keys (also called object properties). While you might think setting an object key equal to undefined would delete it, since undefined is the value that object keys that have not yet been set have, the key would still exist.
You're deleting sjonObj.key
, literally. You need to use array access notation:
delete sjonObj[key];
However, that will also delete where value is equal to 0, since you're not using strict comparison. Use ===
instead:
$.each(sjonObj, function(key, value){
if (value === "" || value === null){
delete sjonObj[key];
}
});
However, this will only walk the object shallowly. To do it deeply, you can use recursion:
(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>
Note that if you're willing to use a library like lodash/underscore.js, you can use _.pick
instead. However, you will still need to use recursion to filter deeply, since neither library provides a deep filter function.
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);
This variant has the added advantage of leaving the original object unmodified, but it does create an entirely new copy, which would be less efficient if you don't need the original object.
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>
Using some ES6 / ES2015:
If you don't like to create an extra function and remove the items 'inline'.
Object.keys(obj).forEach(k => (!obj[k] && obj[k] !== undefined) && delete obj[k]);
jsbin
Same, written as a function.
const removeEmpty = (obj) => {
Object.keys(obj).forEach((k) => (!obj[k] && obj[k] !== undefined) && delete obj[k]);
return obj;
};
jsbin
This function uses recursion to delete items from nested objects as well:
const removeEmpty = (obj) => {
Object.keys(obj).forEach(k =>
(obj[k] && typeof obj[k] === 'object') && removeEmpty(obj[k]) ||
(!obj[k] && obj[k] !== undefined) && delete obj[k]
);
return obj;
};
jsbin
Same as function before but with ES7 / 2016 Object.entries
:
const removeEmpty = (obj) => {
Object.entries(obj).forEach(([key, val]) =>
(val && typeof val === 'object') && removeEmpty(val) ||
(val === null || val === "") && delete obj[key]
);
return obj;
};
Same as third example but in plain ES5:
function removeEmpty(obj) {
Object.keys(obj).forEach(function(key) {
(obj[key] && typeof obj[key] === 'object') && removeEmpty(obj[key]) ||
(obj[key] === '' || obj[key] === null) && delete obj[key]
});
return obj;
};
jsbin
var data = [
{ "name": "bill", "age": 20 },
{ "name": "jhon", "age": 19 },
{ "name": "steve", "age": 16 },
{ "name": "larry", "age": 22 },
null, null, null
];
//eliminate all the null values from the data
data = data.filter(function(x) { return x !== null });
console.log("data: " + JSON.stringify(data));
Building upon suryaPavan's answer this slight modification can cleanup the empty object after removing the invidival emptys inside the object or array. this ensures that you don't have an empty array or object hanging around.
function removeNullsInObject(obj) {
if( typeof obj === 'string' || obj === "" ){
return;
}
$.each(obj, function(key, value){
if (value === "" || value === null){
delete obj[key];
} else if ($.isArray(value)) {
if( value.length === 0 ){
delete obj[key];
return;
}
$.each(value, function (k,v) {
removeNullsInObject(v);
});
if( value.length === 0 ){
delete obj[key];
}
} else if (typeof value === 'object') {
if( Object.keys(value).length === 0 ){
delete obj[key];
return;
}
removeNullsInObject(value);
if( Object.keys(value).length === 0 ){
delete obj[key];
}
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With