我有一个像这样的JSON:
"hotel": [
{
"id": 90091,
"hotelGroup": "A1",
"hotelRoom": [
{
"name":"Room1",
"minimumQuantity": 1,
"maximumQuantity": 6,
},
{
"name":"Room2",
"minimumQuantity": 7,
"maximumQuantity": 12,
}
]},
{
"id": 90089,
"hotelGroup": "A4",
"hotelRoom": [
{
"name":"Room1",
"minimumQuantity": 0,
"maximumQuantity": 0,
}]
}]
我尝试将JSON重新排列为以下格式:
hotel = [{
name: "Room1",
RoomInfo: [{
hotelGroup: "A1",
Stops: [
{"minimumQuantity": 1,"maximumQuantity": 6}
]},
{
hotelGroup: "A4",
Stops: [
{"minimumQuantity": 0,"maximumQuantity": 0}
]
}
]},
{
name: "Room2",
RoomInfo: [{
Name: "A1",
Stops: [
{"minimumQuantity": 7,"maximumQuantity": 12}
]
}]
}]
有人可以提供帮助吗? 我想按酒店名称分组。每个酒店名称都有自己的酒店集团和停靠点。
由于
答案 0 :(得分:0)
显然已将您的JSON转换为常规数组,但您明白了。
const hotel = [{
"id": 90091,
"hotelGroup": "A1",
"hotelRoom": [{
"name": "Room1",
"minimumQuantity": 1,
"maximumQuantity": 6,
},
{
"name": "Room2",
"minimumQuantity": 7,
"maximumQuantity": 12,
}
]
},
{
"id": 90089,
"hotelGroup": "A4",
"hotelRoom": [{
"name": "Room1",
"minimumQuantity": 0,
"maximumQuantity": 0,
}]
}
];
const newHotels = hotel.map(obj => {
const { hotelGroup, hotelRoom } = obj;
const newObj = {};
newObj['name'] = hotelRoom[0]['name'];
newObj['RoomInfo'] = hotelRoom.map(({ minimumQuantity, maximumQuantity }) => {
return {
hotelGroup,
Stops: [{
minimumQuantity,
maximumQuantity
}]
};
});
return newObj;
});
console.log(newHotels);