0

我在主干中有一个应用程序,我想在 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);
});

如何构建这种组合?

4

2 回答 2

3

根据@Bergi 的回答,我想出了这个。它应该可以解决您的问题。

这是一个演示:http ://plnkr.co/edit/NHE9V5?p=preview

更新我已经修改了一些内容以适应您单独的 JSON 文件。

笛卡尔产品助手( http://en.wikipedia.org/wiki/Cartesian_product )

function cartesian(arg) {
  arg = arg || [];
  var r = [],
        max = arg.length - 1;

    function helper(arr, i) {
        for (var j = 0, l = arg[i].length; j < l; j++) {
            var a = arr.slice(0); // clone arr
            a.push(arg[i][j]);
            if (i == max) {
                r.push(a);
            } else helper(a, i + 1);
        }
    }
  if(arg.length > 0)
      helper([], 0);
    return r;
}

嵌套集合解决方案

HotelModel = Backbone.Model.extend({
    initialize: function() {
        // because initialize is called after parse
        _.defaults(this, {
            rooms: new RoomCollection()
        });
    },
    parse: function(response) {
        if (_.has(response, "rooms")) {
            this.rooms = new RoomCollection(response.rooms, {
                parse: true
            });
            delete response.rooms;
        }
        return response;
    },
    toJSON: function() {
        var json = _.clone(this.attributes);
        json.rooms = this.rooms.toJSON();
        return json;
    },
    addRoom: function(rooms, options) {
        return this.rooms.add(rooms, options);
    },
    removeRoom: function(rooms, options) {
        return this.rooms.remove(rooms, options);
    },
    createRoom: function(attributes, options) {
        return this.rooms.create(attributes, options);
    },
    getCombinations: function() {
        return cartesian(_.values(this.rooms.groupBy('level')));
    }
});

RoomModel = Backbone.Model.extend({});

HotelCollection = Backbone.Collection.extend({
    model: HotelModel,
  getAllCombinations: function(){
    return this.map(function(hotel){
      return _.extend(hotel.toJSON(), {
        combinations: hotel.getCombinations()
      });
    });
  }
});

RoomCollection = Backbone.Collection.extend({
    model: RoomModel,
    getRoomsByHotelId: function(hotelId) {
        return this.where({
            hotelId: hotelId
        });
    }
});

加载单独的 JSON

var hotels = new HotelCollection([], {
    url: 'hotels.json'
});
var rooms = new RoomCollection([], {
    url: 'rooms.json'
});

hotels.fetch({
    success: function() {
        rooms.fetch({
            success: function() {
                hotels.each(function(hotel) {
                    hotel.addRoom(rooms.getRoomsByHotelId(hotel.id));
                });
                // all done here
                var combos = hotels.getAllCombinations();
                $(function() {
                    $('body').append('<pre>' + JSON.stringify(combos, null, 2) + '</pre>');
                });
            }
        });
    }
});

酒店.json

[{
  "id": 1,
    "name": "Hotel One"
}, {
    "id": 2,
    "name": "Hotel Two"
}, {
    "id": 3,
    "name": "Hotel Three"
}]

房间.json

[{
  "level": 1,
    "name": "Room A",
    "hotelId": 1
}, {
    "level": 1,
    "name": "Room B",
    "hotelId": 1
}, {
    "level": 2,
    "name": "Room A",
    "hotelId": 1
}, {
    "level": 2,
    "name": "Room B",
    "hotelId": 1
}, {
    "level": 1,
    "name": "Room A",
    "hotelId": 2
}, {
    "level": 1,
    "name": "Room B",
    "hotelId": 2
}, {
    "level": 2,
    "name": "Room A",
    "hotelId": 2
}, {
  "level": 2,
    "name": "Room B",
    "hotelId": 2
}, {
  "level": 1,
  "name": "Room A",
    "hotelId": 3
}, {
  "level": 1,
  "name": "Room B",
  "hotelId": 3
}, {
  "level": 1,
  "name": "Room C",
  "hotelId": 3
}]
于 2013-07-03T17:30:58.683 回答
1

首先,您应该按酒店和级别划分房间列表:

var rooms = _(allRooms.groupBy, "hotel_id");
for (var hotelid in rooms)
    rooms[hotelid] = _.groupBy(rooms[hotelid], "level");

您正在寻找的“组合”是级别的笛卡尔积(对于每个酒店)。例如,您可以使用此辅助函数。像这样使用它:

_.each(this.collection.models, function(hotel) {
    var levels = rooms[hotel.id];
    var combinations = cartesian(_.values(levels));
    // put them on the hotel
});
于 2013-07-02T15:11:01.710 回答