我有一個骨幹應用程序,我想在Json裏面找到一些記錄並打印出來。與骨幹網遞歸函數jQuery的
我的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"
每家酒店可以有一定程度的更多的房間,但我想構建一個房間的foreach酒店組合。
這是我在骨幹解析,但我只檢索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);
});
如何構建這種組合?
是的,如果你可以把它放在骨幹網上就更好,如果你可以進入你的瀏覽器就可以試試它 –
完成!哇,真是一個棘手的問題。 –
+1用於修復我的代碼並將其集成到Backbone中。我應該更好地看待自己的「笛卡兒」功能,我忘記了它不需要陣列(或者至少需要適應)。 – Bergi