I'm currently working on an Angular project and I have a query regarding TypeScript. It's about correctly handling the scenario where a field should not be included in an object if its value is undefined.
In my code, I am initializing an object like this:
let aestheticEvaluation: AestheticEvaluation = {
"altezza": aestheticEvaluationInfo.get('altezza').value,
"peso": aestheticEvaluationInfo.get('peso').value,
// other fields
"notes": aestheticEvaluationInfo.get('notes').value,
};
The issue arises when some fields may have an undefined value. In such cases, these fields should not be added to the aestheticEvaluation object.
For instance, if the value of aestheticEvaluationInfo.get('altezza').value is undefined, the "altezza" field should not be included in the object.
I'm aware that I can use conditional statements to check for null values before adding each field to the object, but this would make the code more repetitive. Is there a way to handle this directly within the object initialization?