从嵌套的json创建一个对象



我从我的服务器收到了以下json,我想从这个json 创建一个自定义对象

{
"showsHall": [
{
"movies": [
"5b428ceb9d5b8e4228d14225",
"5b428d229d5b8e4228d14226",
],
"shows": [
"5b428d6b9d5b8e4228d14229",
"5b428ef69d5b8e4228d1422a",
],
"_id": "5b428d439d5b8e4228d14228",
"hallName": "Small hall",
"rows": 10,
"seats": 40,
}
],
"movie": [
{
"showsHalls": [
"5b428d439d5b8e4228d14228",
"5b43490592bd380e30fef376"
],
"shows": [
"5b428d6b9d5b8e4228d14229",
"5b466d9d75a55841c48bacfc"
],
"_id": "5b428ceb9d5b8e4228d14225",
"movieName": "Moonwalker - Michael jackson",
"directorName": "Jerry Kramer, Colin Chilvers",
"movieDescription": "Anthology movie by, and starring, Michael 
Jackson in his prime, combining a number of...",
}
],
"takenSeats": [],
"_id": "5b428d6b9d5b8e4228d14229",
"showDate": "7/10/2018",
"showStartTime": "4:00 PM",
"showEndTime": "6:00 PM",
}

我想得到的想要的结果应该是:

{       
"hallName": "Small hall",
"movieName": "Moonwalker - Michael jackson" 
"takenSeats": [],
"_id": "5b428d6b9d5b8e4228d14229",
"showDate": "7/10/2018",
"showStartTime": "4:00 PM",
"showEndTime": "6:00 PM",
}

任何帮助都将不胜感激,谢谢

假设您的原始对象名为source,您可以按如下方式组装新对象:

const newObject = {       
"hallName": source.showsHall[0].hallName,
"movieName": source.movie[0].movieName,
"takenSeats": source.takenSeats,
"_id": source._id,
"showDate": source.showDate,
"showStartTime": source.showStartTime,
"showEndTime": source.showEndTime
}
// have a look at it
console.log(newObject);

最新更新