In my case i need a function that remove the null and undefined key parameters from the object a function that transforms this object
Purpose i am creating this long json object from mongo DB i am saving this and and creating XML from this for voice response
{
"say": {
"value": null
},
"play": {
"value": null
},
"dial": {
"value": "7597365803",
"options": {
"action": null,
"answerOnBridge": false,
"callerId": null,
"hangupOnStar": false,
"method": "GET",
"record": true,
"recordingStatusCallback": "false",
"recordingStatusCallbackMethod": "POST",
"recordingStatusCallbackEvent": "completed",
"recordingTrack": "both",
"ringTone": null,
"timeLimit": 14400,
"timeout": 30,
"trim": "do-not-trim",
"_id": "60cc1977c86fe21910ccbc85",
"__v": 0
}
},
"record": {
"value": null
},
"gather": {
"value": null
},
"pause": {
"value": null
},
"redirect": {
"value": null
},
"reject": {
"options": null
},
"number": {
"value": null
},
"user_id": "2",
"hangup": null,
"_id": "60cc0c416349282be4ed2f16",
"__v": 0
}
into this
{
"dial": {
"value": "7597365803",
"options": {
"action": null,
"answerOnBridge": false,
"callerId": null,
"hangupOnStar": false,
"method": "GET",
"record": true,
"recordingStatusCallback": "false",
"recordingStatusCallbackMethod": "POST",
"recordingStatusCallbackEvent": "completed",
"recordingTrack": "both",
"ringTone": null,
"timeLimit": 14400,
"timeout": 30,
"trim": "do-not-trim"
}
}
}
i created this function
function cleanObject (obj: any) {
Object.keys(obj).forEach(k =>
(obj[k] && typeof obj[k] === 'object') && this.cleanObject(obj[k]) ||
(!obj[k] && obj[k] !== undefined) && delete obj[k]
)
return obj
}
but it only solve and some problem and do not run well i tried lodash but it won't help please help me i ma stuck