0

My service returns one array. For example:
return of([1, 2, 3, 4, 5, 6])

I subscribe to it and inside .pipe() I want to separate every 2 values to a different array.
For example:

let result = {
    [1, 2],
    [3, 4],
    [5, 6]
}


How can I achieve this?

Jakub
  • 3
  • 5
  • You should describe what you have tried before asking people for help in Stack Overflow. – Giorgio Tempesta Jun 19 '20 at 11:19
  • You may also want to check [`partition`](https://rxjs.dev/api/index/function/partition) operator. It's not exactly what you need, but something similar, so can be helpful. – Mladen Jun 19 '20 at 11:23

1 Answers1

2

You can use bufferCount to create arrays from your stream:

from([1, 2, 3, 4, 5, 6]).pipe(
  bufferCount(2)
).subscribe(subArray => {
  console.log(subArray); // prints: [1, 2]  [3, 4]  [5, 6]
})
ggradnig
  • 13,119
  • 2
  • 37
  • 61