Linked Questions

Popular Questions

Map operator returning integer as output with array as input

Asked by At

This is example code from the docs. I am new to RXJS, so this might be real easy.

Can anyone explain how map operator is returning a single digit integer after operating on an array?

I checked that the return value of scan operator is an array which increases from [0], [0,1], [0,1,2], [0,1,2,3] .... and so on.

// RxJS v6+
import { interval } from 'rxjs';
import { scan, map, distinctUntilChanged } from 'rxjs/operators';

// Accumulate values in an array, emit random values from this array.
const scanObs = interval(1000)
  .pipe(
    scan((a, c) => [...a, c], []),
    map(r => r[Math.floor(Math.random() * r.length)]),
    distinctUntilChanged()
  )
  .subscribe(console.log);

Related Questions