0
I need to display the result of the sum of my items in one line only.
I made a function to add the values of a list of objects, the result of the sum is given right but it needs to appear in only one row of my table, now it is appearing in several lines.
How can I fix this?
Thank you!
Follows the code:
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>Resultado</th>
</tr>
</thead>
<tbody>
{
this.state.transacoes.map((item, indice) => {
var total = this.state.transacoes.reduce(getTotal, 0);
function getTotal(total, item) {
return total + (item.valor);
}
return (
<tr key={indice}>
<td> {total}</td>
</tr>
)
})
}
</tbody>
</Table>
Code Result:
You’re making the map inside the
render
(orreturn
of a functional component). Why not make a.forEach()
, calculate and then create a line with that result?– Rafael Tavares
@Rafaeltavares is a good one, but I think he could use direct 'reduce' without needing 'map', inside an 'IIFE''
– Cmte Cardeal