0

我在主干中有一个应用程序,我使用该功能来创建笛卡尔积。

这是功能:

    function cartesian(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);
                }
            }
        helper([], 0);
        return r;
    }

要在骨干内部调用它,我这样做:

return cartesian(_.values(this.rooms.groupBy('level')));

这是对象室获取的我的 json 示例:

[{"hotelId":"10776","combination":"0|2|0|0|0","n_camere":2,"level":1,"id":"98%7E%7E%7Edouble_1"},
{"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"99%7E%7E%7Esingle_1"},
{"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"100%7E%7E%7Etriple_1"},
{"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}]

在这种使用笛卡尔积的模式下,我有三个按级别分组的数组,每个数组都有最后一个数组,因为 level2 是这样的:

{"hotelId":"10776","combination":"0|2|0|0|0","n_camere":2,"level":1,"id":"98%7E%7E%7Edouble_1"},
{"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}

---------------
    {"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"99%7E%7E%7Esingle_1"},
{"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}

---------------
    {"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"100%7E%7E%7Etriple_1"},
{"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}

. 我想要实现的是分组级别和组合,因为在级别 1 中有一个具有相同组合字段的数组,而我的目标是一个这样的数组:

{"hotelId":"10776","combination":"0|2|0|0|0","n_camere":2,"level":1,"id":"98%7E%7E%7Edouble_1"},
    {"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}

    ---------------
        {"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"99%7E%7E%7Esingle_1"},
{"hotelId":"10776","combination":"1|0|1|0|0","n_camere":2,"level":1,"id":"100%7E%7E%7Etriple_1"},
    {"hotelId":"10776","combination":"1|0|0|0|0","n_camere":1,"level":2,"id":"98%7E%7E%7Edouble_2"}

有可能得到这个结果吗?

4

0 回答 0