我在主干中有一个应用程序,我想在 Json 中找到一些记录并打印出来。
我的 JSON 是这样的:
[
{
"id" : "r1",
"hotel_id" : "1",
"name" : "Single",
"level" : "1"
},
{
"id" : "r1_1",
"hotel_id" : "1",
"name" : "Double",
"level" : "2"
},
{
"id" : "r1_3",
"hotel_id" : "1",
"name" : "Double for single",
"level" : "1"
},
{
"id" : "r1_4",
"hotel_id" : "1",
"name" : "Triple",
"level" : "3"
},
{
"id" : "r2",
"hotel_id" : "2",
"name" : "Single",
"level" : "1"
},
{
"id" : "r2_1",
"hotel_id" : "2",
"name" : "Triple",
"level" : "1"
}
]
我想将每个酒店的每个房间组合起来以达到水平。每家酒店可以有更多的房间组合,但有独特的层次。我的目标是为 id = 1 的酒店打印类似的内容(对于另一个具有不同组合的相同): id 为 1 的酒店的第一个组合:
Room "Single", "level" : "1" , "hotel_id" : "1"
Room "Double", "level" : "2" , , "hotel_id" : "1"
Room "Triple", "level" : "3" , , "hotel_id" : "1"
id 为 1 的酒店的第二种组合:
Room "Double for single", "level" : "1" , "hotel_id" : "1"
Room "Double", "level" : "2" , , "hotel_id" : "1"
Room "Triple", "level" : "3" , , "hotel_id" : "1"
每家酒店都可以有更多的房间,但我想为每家酒店构建一个房间的组合。
这是我在主干中的解析,但我只检索了 allRooms 中的 JSON。
//each for all my hotel
_.each(this.collection.models, function(hotel) {
var rooms = new Array();
rooms.push(allRooms.where({hotel_id : hotel.id}));
//this is where I have to construct my combination
//this is the array for each combination
hotel.get('rooms').push(rooms);
});
如何构建这种组合?