I have below JSON. how to remove the values which contain blank value or null value using Javascript?
dataJSON==
{
"Cli Info:Sub_Ind":""
"Cli Info:Sub_Mat":"",
"Cli Info:Sub_Ewwu":"",
"Cli Info:Sub_kjlo":"",
"Cli Info:Sub_uti":"",
"S Funds:S_C_origin":"Afghanistan;Albania",
"S We:Count_we":"Antigua & Barbuda;American Samoa;Austria",
"Add Juri:Jurid_Coun":"Andorra;Angola"
}
i tried the approach but not working.
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === null || obj[propName] === undefined) {
delete obj[propName];
}
}
return obj
}
I am expecting to remove all the null,blank and undefined values from the JSON
3
Answers
you can use replacer in the JSON.stringify
here you can write your own custom replacer function
This removes null and undefined values with a for in loop (PURE Javascript):
Note: Feel free to add the empty string on the if statement.
Use your code, have an empty object literal, and use
Object.assign
inside the loop as an option. Change your condition to check not-nullish values.