使用破坏和对象格式化。
我正在启动一个新的React项目,我的API返回了我的JSON,其中这种格式不是我想要的会议。我尝试使用破坏性和object.entries
。我拥有的:
{
"meetings": [
{
"room1": DC13,
"room2": AP47,
}
],
"teachers": [
{
"name: "Patrick",
"speciality":"Math"
},
...
],
"alumni": [
{
"firstName": "Joe",
"lastName":"Luis"
},
...
]
}
我想要的:
{
"meetings": [
{
"name": room1,
"location": DC13,
},
{
"name": room2,
"location": AP47,
},
],
"teachers": [
{
"name: "Patrick",
"speciality":"Math"
},
...
],
"alumni": [
{
"firstName": "Joe",
"lastName":"Luis"
},
...
]
}
我尝试过但不起作用的是:
const data= {meetings, teachers, alumni}
const newData = Object.entries(meetings).reduce((acc,name,location])=>acc.concat({name,location}),[]))
然后使用此newdata进行操作。
I'm starting a new project in React and my API returns me a JSON where the format is not what I want for meetings. I've tried to use destructuring and Object.entries
. What I have:
{
"meetings": [
{
"room1": DC13,
"room2": AP47,
}
],
"teachers": [
{
"name: "Patrick",
"speciality":"Math"
},
...
],
"alumni": [
{
"firstName": "Joe",
"lastName":"Luis"
},
...
]
}
What I want:
{
"meetings": [
{
"name": room1,
"location": DC13,
},
{
"name": room2,
"location": AP47,
},
],
"teachers": [
{
"name: "Patrick",
"speciality":"Math"
},
...
],
"alumni": [
{
"firstName": "Joe",
"lastName":"Luis"
},
...
]
}
What I've tried but not working:
const data= {meetings, teachers, alumni}
const newData = Object.entries(meetings).reduce((acc,name,location])=>acc.concat({name,location}),[]))
and then use this newData for manipulation after.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来,使用
MAP
此处比redable
更干净,因此,这是使用MAP
的工作解决方案。It seems that using
map
here would be cleaner thanreduce
, so here's a working solution usingmap
.这为您带来了所需的输出。它通过将其输入映射到新的对象中,用
name
和位置
键将对象映射并重组对象。This gets you the desired output. It maps it each meeting and restructures the object by mapping its entry into a new object with the
name
andlocation
keys.