I want to create a method to delete null and undefined attributes.
To do that :
I convert my plain object into table
loop on it
filter on attributes if there are null or undefined
But the thing is I don't arrive to restructure my object with the new values. Let's take a look :
var req = {
adtForm1: {
firstName: 'John',
lastName: undefined,
middleName: ''
},
adtForm2: {
firstName: null,
lastName: 'Doe',
middleName: ''
}
};
removeUndefinedFormsAttributes = (somesForms) => {
const forms = Object.values(somesForms);
const formsUpdate = {};
forms.forEach(item => {
const formFields = Object.values(item);
formFieldsKeys = Object.keys(item);
const formFiltered = formFields.filter(field => { return field !== null && field !== undefined; });
console.log("formFiltered", formFiltered);
})
console.log(somesForms)
return forms;
};
removeUndefinedFormsAttributes(req)
As we can see in the snippet, formFiltered change the good values but I need to return the same object as somesForms. This is what I need :
expectedValue = {
adtForm1: {
firstName: 'John',
middleName: ''
},
adtForm2: {
lastName: 'Doe',
middleName: ''
}
}
I know I need to use reduce() function and keys() function but truly I don't know how. I will really appreciate any help.