I receive such a set of data:
const data = [
{country_code: 'pl', postal_code: '56-200', city: 'foo'},
{country_code: 'de', postal_code: '22345', city: null}
];
and I would like to cut null values out of it so it looks like this:
const parsedResult = [
{country_code: 'pl', postal_code: '56-200', city: 'foo'},
{country_code: 'de', postal_code: '22345'}
];
I have written a function that works but I would like to do it better so that it is not dependent on further parameters:
return data.map(({ country_code, postal_code, city }) => {
const preparedObject = {};
if (country_code) {
preparedObject.country_code = country_code;
}
if (postal_code) {
preparedObject.postal_code = postal_code;
}
if (city) {
preparedObject.city = city;
}
return preparedObject;
});
}