我在骨干网上有一个应用程序,我使用该功能来创建笛卡尔积。
这是功能:
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"}
。 我要实现的是groupby级别和组合,因为在级别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"}
是否可以获得此结果?