Asdrubal Hernandez
Asdrubal Hernandez

Reputation: 263

Get an array of 4 elements with RxJS

Trying to get data from firebase, but grouped 4 by 4. How would I manage to group and return an observable of 4 in 4.

# input
# [1, 2, 3, 4, 5, 6, 7, 8]

# output
# [ [1,2,3,4], [5, 6, 7, 8]...]

I have tried this, but it only takes the first 4 and returns an array and not as an observable

this.data$ = this.afs
      .collection<Data>('data')
      .valueChanges()
      .pipe(take(4), toArray());

Upvotes: 1

Views: 179

Answers (2)

Mrk Sef
Mrk Sef

Reputation: 8022

Use bufferCount

this.data$ = this.afs
  .collection<Data>('data')
  .valueChanges()
  .pipe(bufferCount(4));

Upvotes: 2

fippi
fippi

Reputation: 534

I think you're explicitly making the return type an array by using .pipe(take(4), toArray());. Try removing the second parameter toArray().

Upvotes: 0

Related Questions