0
Staff I set up a table with the data of an array where will have attack and defense points of each player associated to a team. You will have the total points of attack and defenses of each player and a final total that adds up the total of attack and the total of defense of each player. Below an image to understand what I am saying:
My problem is I want to sort the "SUM A/B" column down. I know javascript has the Sort method, but can not use for my problem. Below an image of how I want it to look.
Thank you if someone will at least give me an idea. My code below:
console.clear()
const Debug = () => {
const [players, setPlayers] = React.useState([
{
name: 'Tiago',
teams: [
{ name: 'Invictos', attack: 18, defense: 5 },
{ name: 'Warrions', attack: 10, defense: 2 },
],
},
{
name: 'Carlos',
teams: [
{ name: 'Invictos', attack: 13, defense: 7 },
{ name: 'Warrions', attack: 9, defense: 3 },
],
},
{
name: 'Ana',
teams: [
{ name: 'Invictos', attack: 18, defense: 8 },
{ name: 'Warrions', attack: 17, defense: 5 },
],
},
])
return (
<div>
<table style={{ textAlign: 'center', border: '1px solid #000' }}>
<thead>
<tr>
<th rowSpan="2">#</th>
<th rowSpan="2">Nome</th>
<th colSpan="2">Invictos</th>
<th colSpan="2">Warrions</th>
<th colSpan="2">Total</th>
<th>Soma A/D</th>
</tr>
<tr>
<td>Ataques</td>
<td>Defesas</td>
<td>Ataques</td>
<td>Defesas</td>
<td>Ataques</td>
<td>Defesas</td>
</tr>
</thead>
<tbody>
{players.map((player, index) => (
<tr key={index}>
<td>{index + 1}</td>
<td>{player.name}</td>
{player.teams.map((p, i) => (
<React.Fragment key={i}>
<td>{p.attack}</td>
<td>{p.defense}</td>
</React.Fragment>
))}
<td>{player.teams.reduce((a, b) => a + b.attack, 0)}</td>
<td>{player.teams.reduce((a, b) => a + b.defense, 0)}</td>
<td>
{player.teams.reduce((a, b) => a + b.attack, 0) +
player.teams.reduce((a, b) => a + b.defense, 0)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)
}
ReactDOM.render(<Debug />, document.querySelector('#root'))
<div id="root"></div>
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
increasing or decreasing? You spoke one thing and in the image you showed another. Why not create a new state that stores objects with calculated values and then render from this new object?
– Cmte Cardeal
Hello friend, it’s even decreasing, already fix was typo. I thought about it, but couldn’t find a way to concatenate with the rest of the data. Could you show me an example?
– Bruno Lucas