I need to convert the posters object below to the normal array of objects but my code is not looking best any way to convert it proper in TypeScript.
and I don't know what a poster's object structure is called?
const posters = {
facebook: ['aaa', 'bbb', 'ccc'],
twitter: ['aaa', 'bbb', 'ccc'],
instagram: ['aaa', 'bbb', 'ccc'],
youtube: null,
forum: null,
};
If some entity has null value will be not returned to the new object, an object will be like this.
const posterTransformed = [
{ platform: 'facebook', name: ['aaa', 'bbb', 'ccc'] },
{ platform: 'twitter', name: ['aaa', 'bbb', 'ccc'] },
{ platform: 'instagram', name: ['aaa', 'bbb', 'ccc'] },
];
And this is my code
const transformPosters= (posters: any) => {
const results = [];
if (posters?.facebook)
results.push({ platform: 'facebook', name: posters?.facebook || [] });
if (posters?.twitter)
results.push({ platform: 'twitter', name: posters?.twitter || [] });
if (posters?.instagram)
results.push({ platform: 'instagram', name: posters?.instagram || [] });
if (posters?.youtube)
results.push({ platform: 'youtube', name: posters?.youtube || [] });
return results;
};
Object.entries(posters).filter(([k, v]) => v).map(([k, v]) => ({ platform: k, name: v }));