When working with an Angular 4 App and a typescript model, I have defined a Person class as follows:
export class Person{
fname:string,
lname?:string
}
The 'lname' property in the model is optional. To populate the model in a component, I use the following code:
//Query form data
var people = form.get('people');
let model = new Person(){
fname: people.get('firstname'),
lname: people.get('lastname')
}
In this scenario, if the user does not enter a value for 'lastname', the resulting JSON will include a null value for 'lname':
{'fname': 'xyz', 'lname': null}
Expected Result:
To avoid having null properties in the JSON output, I want it to look like this:
{'fname':'xyz'}
However, when the user does enter a value for 'lname', the JSON should include both values:
{'fname':'xyx', 'lname': 'abc'}
I am looking for a way to achieve this desired JSON result from my TypeScript model.