Recursive function jquery with backbone

后端 未结 2 1064
逝去的感伤
逝去的感伤 2021-01-26 13:40

I have an app in backbone where I want to find inside a Json some records and print out.

my JSON is like this:

[
  {
    \"id\" : \"r1\",
    \"hotel_id\         


        
2条回答
  •  礼貌的吻别
    2021-01-26 14:08

    First you should split your rooms list by hotels and levels:

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

    The "combinations" you're looking for is the cartesian product of the levels (for each hotel). You can use this helper function for example. Use it like this:

    _.each(this.collection.models, function(hotel) {
        var levels = rooms[hotel.id];
        var combinations = cartesian(_.values(levels));
        // put them on the hotel
    });
    

提交回复
热议问题