1

我正在尝试为板球比赛模拟生成overs,其值如下:

Overs -> 0 - 19(共 20 个)
Balls -> 1 - 6(共 120 个,每个 Over 有 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...

我在这段代码中缺少什么?

此外,如果您想在浏览器中尝试并试验它,请参考上述代码的 repl:https ://repl.it/@ishankhare07/cricket-overs

4

1 回答 1

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));

现场演示:https ://stackblitz.com/edit/rxjs-aud3ot

于 2019-03-15T10:02:59.987 回答