I have a code snippet in JS I'm altering. The original has an object called rows defined by:
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];
And displayed with
<TableBody>
{rows.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
I'm trying to move all the HTML into a function:
function my_rows() {
return rows.map((row) => (
<TableRow key={row.name}>
<TableCell component='th' scope='row'>
{row.name}
</TableCell>
{ Object.values(row).forEach(v => <TableRow align='right'>{v}</TableRow> )}
</TableRow>
))
}
but my forEach gives back nothing...
- How can this be achieved (this being "generating all the correct HTML tags for each value of the object
row")? - Is this a good idea? (My thought was this: Have a python-flask server generating this
rowobject, and let JS display the whole object, without knowing beforehand which and how much values this object has)
TableCells but you are iterating forTableRows in your second example.