reactjs - 如何映射对象
问题描述
我有个问题。我想将响应对象映射到数组。基本上,如果响应也是一个数组,则使用映射,但在这种情况下,来自服务器的响应是一个对象。我该如何映射?
获取用户.js
this.state = {
post : '',
post2 : [],
formData : {
username : '',
clientId : ''
}
getPostAPI = (event) => {
event.preventDefault();
const token = localStorage.getItem('token');
const { post } = this.state
const { username } = this.state.formData
axios.post(`http://10.40.22.21:8062/users/get`, { username } ,
{
headers:
{
"Content-Type" : "application/json",
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"AUTHENTICATION-KEY" : token,
}
})
.then((res)=>{
let response = res;
if(response){
this.setState ({
post : response.data.response
})
console.log(response.data.response)
}
},this)
.catch( (error) => {
alert('Username Tidak di Temukan',error)
}
)
}
GETuser.js 中的 myForm
{
Object.values(post).map((h,i) => (
<tr key = {i} >
<th scope="row">
<Media className="align-items-center">
<Media>
<span className="badge-dot mr-4">
{h.username}
</span>
</Media>
</Media>
</th>
<th scope="row">
<Media className="align-items-center">
<Media>
<span className="badge-dot mr-4">
{h.firstName}
</span>
</Media>
</Media>
</th>
<th scope="row">
<Media className="align-items-center">
<Media>
<span className="badge-dot mr-4">
{h.password}
</span>
</Media>
</Media>
</th>
<th scope="row">
<Media className="align-items-center">
<Media>
<button className="btn btn-danger" onClick= {() => this.handleRemove(post.clientId)}><i class="fa fa-trash"></i></button>
</Media>
</Media>
</th>
</tr>
))
来自服务器的响应
{
"responseCode":"00",
"message":"Process successful. ",
"localMessage":"Proses Berhasil.",
"response" : {"username":"admin","firstName":"admin","middleName":"is","lastName":"admin","password":"fZE+K9bKT29FAEOaJbfFw==","effectiveDate":"25-01-2019 07:00:00","expiredDate":"25-01-2021 07:00:00","phone":"082234051157","email":"asdsad@gmail.com"}}
我希望不能 post.map,所以我可以使用操作删除或更新谢谢
解决方案
如果要将对象转换为数组,则有以下选项:
1)Object.values(post.response)
将对象的值作为数组提供给您。
2)Object.keys(post.response)
会给你对象的键作为一个数组。
3)Object.entries(post.response)
将为您提供一个数组,其中每个元素都是一个由键和值组成的数组。
因此,您可以这样做:
Object.keys(response).map( heading =>
<tr>
<th scope="row">
<Media className="align-items-center">
<Media>
<span className="badge-dot mr-4">
{heading}
</span>
</Media>
</Media>
</th>
<td scope="row">
<Media className="align-items-center">
<Media>
<span className="badge-dot mr-4">
{response[heading]}
</span>
</Media>
</Media>
</td>
这样,您可以根据对象属性动态定义列并输出相应的值。
推荐阅读
- c# - nunit3-console 运行期间出现 FileNotFoundException
- time-complexity - 证明或反驳时间复杂度
- c# - 如何修复:“在 MS SQL Server 重新启动 alipne .net core 2.2.5 容器后,使用它的容器有一个 CLOSE_WAIT tcp 连接 - dotnet 的 CPU 上升”
- java - 无法访问类 com.sun.javafx.scene.control.skin.resources.ControlResources(在模块 javafx.controls 中)
- gitlab-ci - 如何在 gitlab CI 管道期间在构建主机(不是 docker 容器)上运行脚本
- json - 使用 jq Azure DevOps Bash 任务替换 json 文件中的值会添加随机数据
- sql - 如何从当月获取前 12 个月的最后一天数据
- java - 如何在 apache camel 中正确使用直接组件?
- wcf - WCF 服务值错误不在预期范围内
- amazon-web-services - 如何使用 jenkins 构建并在 aws 上部署?