[英]React & JSX: Nested Object.values inside an HTML Table
I have two objects that I'm converting to arrays via Object.values().map
.我有两个对象,我正在通过Object.values().map
。 I have a Material-UI
table and I want to iterate the arrays and display table data from the arrays.我有一个Material-UI
表,我想迭代 arrays 并显示 arrays 中的表数据。
The two arrays are players
and stats
.两个 arrays 是players
和stats
。 I want to access data from players
in the first couple of columns in the table, access data from stats
in the next couple of columns, and then again access data from players
in the last couple of columns in the form.我想在表的前几列中访问来自players
的数据,在接下来的几列中访问来自stats
数据的数据,然后在表格的最后几列中再次访问来自players
的数据。 In other words, stats
is sandwiched between players
in the table columns.换句话说, stats
夹在表列中的players
之间。
Here is what I tried:这是我尝试过的:
{Object.values(player).map((player, key) => (
<TableRow>
<TableCell align="left" className={classes.tableContent}>{player[0].primaryNumber}</TableCell>
<TableCell align="left" component="th" scope="row" className={classes.tableContent}>
{player[0].fullName}
</TableCell>
<TableCell align="left" className={classes.tableContent}>{player[0].primaryPosition.name}</TableCell>
// Getting an error because this .map is nested inside the player .map
{Object.values(stat).map((stat, key) => (
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.points}</TableCell>
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.goals}</TableCell>
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.assists}</TableCell>
))}
<TableCell align="left" className={classes.tableContent}>{player[0].currentAge}</TableCell>
<TableCell align="left" className={classes.tableContent}>{player[0].height}</TableCell>
<TableCell align="left" className={classes.tableContent}>{player[0].weight}</TableCell>
<TableCell align="left" className={classes.tableContent}>{player[0].nationality}</TableCell>
</TableRow>
))}
How can I successfully nest the inner Object.values(stat).map
inside the outer Object.values(players).map
?如何成功地将内部Object.values(stat).map
嵌套在外部Object.values(players).map
? Is there a cleaner way to do this?有没有更清洁的方法来做到这一点?
You have to do what is stated in the error.您必须按照错误中的说明进行操作。 Try this;试试这个;
{Object.values(stat).map((stat, key) => (
<>
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.points}</TableCell>
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.goals}</TableCell>
<TableCell align="left" className={classes.tableContent}>{stat[0].splits[0].stat.assists}</TableCell>
</>
))}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.