Are there any other cleaner approach for my code? Looks my code isn’t readable.
const materials = entry.materials.flat().map(c => ({
...c,
...(c.quality_id ? { quality_id: c.quality_id } : { quality_id: null }),
...(c.quantity_used
? { quantity_used: c.quantity_used }
: { quantity_used: 0 }),
...(c.remarks ? { remarks: c.remarks } : { remarks: "" }),
}));
>Solution :
You don’t need to spread the objects other than the first c.
const materials = entry.materials.flat().map(c => ({
...c,
quality_id: c.quality_id || null,
quantity_used: c.quantity_used || 0,
remarks: c.remarks || ''
}));