Get angular Observable stream as array

前端 未结 3 2131
清酒与你
清酒与你 2021-02-14 09:52

I have list, that should simulate the stream :

list = [
  {name : \'Str1\', age: 10},
  {name : \'Str2\', age: 10},
  {n         


        
相关标签:
3条回答
  • 2021-02-14 10:28

    Have you tried to use toArray operator?.

    let list = [
      { name: 'Str1', age: 10 },
      { name: 'Str2', age: 10 },
      { name: 'Str3', age: 10 },
      { name: 'Str4', age: 10 },
      { name: 'Str5', age: 10 },
      { name: 'Str6', age: 10 }
    ];
    
    let currentPage = 2;
    let pageSize = 2;
    
    Rx.Observable.from(list)
      .skip(currentPage * pageSize)
      .take(pageSize)
      .toArray()
      .subscribe(data => console.log(data), console.error);
    <script src="https://npmcdn.com/@reactivex/rxjs@5.0.3/dist/global/Rx.min.js"></script>

    0 讨论(0)
  • 2021-02-14 10:31

    The scan operator should do what you want

    Observable.from(this.list)
    .skip(this.currentPage * this.pageSize)
    .take(this.pageSize)
    .scan([], acc, curr) => {acc.push(curr); return acc;});
    .subscribe(data => this.pagerList = data, console.error);
    

    http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan

    0 讨论(0)
  • 2021-02-14 10:47

    I think this is more appropriate solution:

    Observable.of(this.list).map(x => x.slice(this.currentPage * this.pageSize)).map(x => x.slice(0, this.pageSize)).subscribe(data => this.pagerList = data, console.error);
    
    0 讨论(0)
提交回复
热议问题