Reputation: 9
So I am trying to remove all the empty/null/spaces/undefined spaces in this JSON, but I am stuck. This is the code I have so far:
<!DOCTYPE html>
<html>
<head>
<title>
JSON Example
</title>
</head>
<style type="text/css">
body {
padding: 20px;
font-family: Helvetica;
}
</style>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
var data = {
"first_name": "Sick",
"last_name": "Nick",
"email": "sick,[email protected]",
"gender": null,
"invitations": [
{
"from": "",
"code": null
}
],
"company": {
"name": "",
"industries": []
},
"address": {
"city": "Minnesota",
"state": "MN",
"zip": "14221",
"street": " "
}
};
//eliminate all the null values from the data
function clean(obj) {
var clone = JSON.parse(JSON.stringify(obj))
console.log("The clone is: "+clone);
for (var propName in obj) {
var toRemove = obj[propName];
if (toRemove === null || toRemove === [] || toRemove === {} || toRemove === "" || toRemove === " ") {
obj.splice(obj.indexOf(propName),1);
}
}
return obj;
}
clean(data);
</script>
<p>"The value is: "<span id="data"></span></p>
</body>
</html>
As you can see I have a preliminary function written for it, but I don't quite grasp the idea fully. Can anyone help me by pointing me in the right direction?
Upvotes: 1
Views: 77
Reputation: 12637
If you have to do so much cleanup, why even bother with JSON.parse(JSON.sringify(data))
in the first place? Ain't the purpose of this construct to have a quick and easy way to make a deep clone without the need to iterate over the objects yourself? But you have to, to clean up your clone.
check this out:
var data = {
"first_name": "Sick",
"last_name": "Nick",
"email": "sick,[email protected]",
"gender": null,
"invitations": [{
"from": "",
"code": null
}],
"company": {
"name": "",
"industries": []
},
"address": {
"city": "Minnesota",
"state": "MN",
"zip": "14221",
"street": " "
}
};
function clone(value) {
if (typeof value === "object") {
let _clone;
if (Array.isArray(value)) {
for(let v of value){
let w = clone(v);
if(w !== undefined){
_clone || (_clone = []);
_clone.push(w);
}
}
} else if (value !== null) {
for (let key of Object.keys(value)) {
let w = clone(value[key]);
if (w !== undefined) {
_clone || (_clone = {});
_clone[key] = w;
}
}
}
return _clone;
}
if (typeof value === "string") {
return value.trim() ?
value :
undefined;
}
return value;
}
console.log(clone(data));
.as-console-wrapper{top:0;max-height:100%!important}
Upvotes: 1
Reputation: 1652
You can use the second parameter of JSON.stringify for apply the clean up for the object or simply clean up your object with a function like this
function isArray(arr){
return Object.prototype.toString.call(arr) === '[object Array]';
}
function isObject(obj){
return Object.prototype.toString.call(obj) === '[object Object]';
}
function clean(obj){
if(typeof obj === 'string' || typeof obj === 'number'){
return obj;
}
if (isArray(obj)){
var arr = [];
for(var i = 0; i < obj.length; i++){
var value = obj[i];
if (value){
if (isArray(value) && value.length > 0){
arr.push(clean(value))
}
if(!isArray(value)){
arr.push(value)
}
}
}
return arr;
}
var object = {};
for(var key in obj){
var val = obj[key];
if((isArray(val) && val.length > 0) || isObject(val)){
object[key] = clean(val);
}
if((typeof val === 'string' && val) || typeof val === 'number'){
object[key] = clean(val)
}
}
return object;
}
var obj = {
name: '',
arr:[],
name2: 'yode',
age: 27,
nested: {
name: 'myname'
}
}
var d = clean(obj);
alert(JSON.stringify(d))
I hope It can help you
Upvotes: 0