我正在尝试为板球比赛模拟生成以下值:
Overs-> 0-19(共20个overs)
球-> 1-6(共120个,每个上方有6个球)
因此,必须生成的序列类似于0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.1, 1.2, 1.3, ....., 19.4, 19.5, 19.6
我可以通过使用2个嵌套循环进行迭代来轻松实现此目的,但是由于我将RxJS与angular一起使用,因此我想以反应方式进行此操作。我想出了以下应该可以在AFAIK上运行的代码:
import { range } from 'rxjs';
import { map, combineAll } from 'rxjs/operators';
let overs = range(0, 20);
let balls = range(1, 6);
let ob = overs.pipe(
map((over) => balls.pipe(
map((ball) => `${over}.${ball}`)
))
).pipe(combineAll())
ob.subscribe((val) => console.log(val));
但是问题是,它缺少很多值。我得到以下输出:
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.1' ]
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.2' ]
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.3' ]
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.4' ]
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.5' ]
[ '0.6',
'1.6',
'2.6',
'3.6',
'4.6',
'5.6',
'6.6',
'7.6',
'8.6',
'9.6',
'10.6',
'11.6',
'12.6',
'13.6',
'14.6',
'15.6',
'16.6',
'17.6',
'18.6',
'19.6' ]
正如我们可以看到的那样,所有的溢出都已生成(0-19),但是每个都只得到.6
值。只有倒数第二个,即19th
具有唯一值19.1, 19.2...
此代码中我缺少什么?
此外,如果有帮助,如果您想在浏览器中尝试使用上述代码,请参见以下代码:https://repl.it/@ishankhare07/cricket-overs
答案 0 :(得分:2)
您甚至不需要使用combineAll
而是使用mergeMap
来简化理解。
import { range } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
let overs = range(0, 20);
let balls = range(1, 6);
let ob = overs.pipe(
mergeMap((over) => balls.pipe(
map((ball) => `${over}.${ball}`),
)),
);
ob.subscribe((val) => console.log(val));