我有一个骨干应用程序,我想在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