javascript - ReactJs 渲染对象的值(对象数组)
问题描述
{
"pendingShareholder": [
[{
"id": 5351,
"userName": "iverson",
"firstName": "Allen",
"lastName": "Iverson",
"password": "$2a$10$20ILdapdX6u8G1nH2jIr6upiKY04LCxD9yjHKUHRhUfpuG1w1ywd2",
"contact": "97801671",
"email": "john.doe@gmail.com",
"roles": [{
"id": 3,
"name": "ROLE_SHAREHOLDER"
}]
},
{
"id": 5951,
"userName": "rosgeller",
"firstName": "Ros",
"lastName": "Geller",
"password": "$2a$10$Udrju2Tj6mKGJRZA3d2GFer6kfSI988xI1/R50s.XmrHcIN1IJxoO",
"contact": "90908899",
"email": "peter.sou@gmail.com",
"roles": [{
"id": 3,
"name": "ROLE_SHAREHOLDER"
}]
}
]
]
}
大家好
我有上面的对象。如您所见,它是一个对象,具有一个键(“pendingShareholder”)。这个键的值是一个数组内的一个数组(里面有对象)。
我需要在 ReactJS 中返回它。在这里使用 React Hooks。无论如何我似乎都做不到:(任何人都可以帮助我。我要疯了
提前致谢!
const adminStateObject = useSelector((state) => state.admin11111);`
return (
<div className="adminmaincontent">
<h2>Pending Approval Users</h2>
<Table striped bordered hover size="sm" responsive>
<thead>
<tr>
<th>ID</th>
<th>UserName</th>
<th>First Name</th>
<th>Last Name</th>
<th>Contact</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
{Object.keys(adminStateObject).map((key, i) => (
<tbody key={uuid()}>
{adminStateObject[key].map((key2, i2) => (
<tr key={uuid()}>
<td key={uuid()}>{key2.id}</td>
<td key={uuid()}>{key2.userName}</td>
<td key={uuid()}>{key2.firstName}</td>
<td key={uuid()}>{key2.lastName}</td>
<td key={uuid()}>{key2.contact}</td>
<td key={uuid()}>{key2.email}</td>
</tr>
))}
</tbody>
))}
</Table>
</div>
);
解决方案
map
看起来您正在进行的操作太多。您需要一个迭代嵌套数组的方法。
正如 Gulam 在他们的评论中提到的那样,在每个tbody
,tr
和上使用 uuidtd
是矫枉过正的。只需添加对象 id 作为行键。
function Example({ data }) {
return (
<table>
<thead>
<tr>
<th>ID</th>
<th>UserName</th>
<th>First Name</th>
<th>Last Name</th>
<th>Contact</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{data.pendingShareholder[0].map(obj => (
<tr key={obj.id}>
<td>{obj.id}</td>
<td>{obj.userName}</td>
<td>{obj.firstName}</td>
<td>{obj.lastName}</td>
<td>{obj.contact}</td>
<td>{obj.email}</td>
</tr>
))}
</tbody>
</table>
);
};
const data = {pendingShareholder:[[{id:5351,userName:"iverson",firstName:"Allen",lastName:"Iverson",password:"$2a$10$20ILdapdX6u8G1nH2jIr6upiKY04LCxD9yjHKUHRhUfpuG1w1ywd2",contact:"97801671",email:"john.doe@gmail.com",roles:[{id:3,name:"ROLE_SHAREHOLDER"}]},{id:5951,userName:"rosgeller",firstName:"Ros",lastName:"Geller",password:"$2a$10$Udrju2Tj6mKGJRZA3d2GFer6kfSI988xI1/R50s.XmrHcIN1IJxoO",contact:"90908899",email:"peter.sou@gmail.com",roles:[{id:3,name:"ROLE_SHAREHOLDER"}]}]]};
// Render it
ReactDOM.render(
<Example data={data} />,
document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
推荐阅读
- azure - 从工作簿固定时,Azure 仪表板未使用新时间范围进行更新
- modeling - 如何在 Xpress IVE 中声明约束?
- javascript - 如何解决迭代中的错误元素期望在Vuejs中有'v-bind:key'指令vue / require-v-for-key?
- java - 没有 TLS 的 Spring FTP 客户端连接
- rgb - 如何将数字(值)转换为 rgb?
- google-colaboratory - 如何阻止谷歌协作断开连接?
- r - 计算另一个因子内的因子水平 - R
- reactjs - 指定的“redirect_uri”对此客户端应用程序无效
- python - 为什么不显示 OGR 层?
- wordpress - Wordpress Mailpoet 3 不在 Outlook 中显示图像