Is there any way to convert all undefined, NaN, etc. values in a Javascript object to a blank string (so it's at least defined).
ex: javascriptObject = defineUndefined(javascriptObject)
Something that works like that.
Is there any way to convert all undefined, NaN, etc. values in a Javascript object to a blank string (so it's at least defined).
ex: javascriptObject = defineUndefined(javascriptObject)
Something that works like that.
One idea might be to check if a value is a NaN, and set that equal to 0 (or a value of your choosing).
if (isNaN(x)) x = 0;
For more information on isNaN, you can take a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
demo_object = {
string: 'non empty string',
emptyString: '',
nullString: null,
zero: 0,
nonZero: 10,
negativeNumber: -10,
undefinedValue: undefined,
notANumber: NaN
}
Use the below code to convert null, NAN, undefined and 0 to Blank String of demo_object.
Object.keys(demo_object).map((key)=>{
if(!demo_object[key]){
demo_object[key] =''
}
});
Below is Updated demo_object
demo_object = {
string: 'non empty string',
emptyString: '',
nullString: '',
zero: '',
nonZero: 10,
negativeNumber: -10,
undefinedValue: '',
notANumber: ''
}