-1

i have following code written in rxjs5 and it broke with rxjs6

can some one help me with write in rxjs 6

its failing mergemap receiving groupedObserable which does not have count method and along also filter method does not exist.

list [

{id: '1', type: 't1',  name: 'f1', des:'d1', selected: true},
{id: '2', type: 't1',  name: 'f2', des:'d2', selected: false},

{id: '3', type: 't1',  name: 'f11', des:'d11', selected: false},
{id: '4', type: 't1',  name: 'f22', des:'d22', selected: true},
]

Observable.from(list)
.filter(a => a.name != null)
.groupBy(i => i.type)
.mergeMap(list => {
let count = list.count;
let selectedCount = 0;
list.filter( f => f.selected).count.subscribe(c => selectedCount = c)
return count.map(count =>  {
   {
     key: list.key,
     totalCount: count,
     selected: selectedCount
   }
}
}).reduce((x, y) => {
x.isValid = x.selectedCount > 0 
return x;
}).subscribe(r => {
  console.log(r + 'any item selected')
}
)

when i tried to write in rxjs6 only progress i was able to made till here thanks in advance.

from(list)
    .pipe(
    filter( s=> s.name != null) ,
    groupBy(i => i.type),
    mergeMap( (value, index) => {
      value.count // that's where it starts to fail
    }
    ))
d-man
  • 57,473
  • 85
  • 212
  • 296

1 Answers1

1

The equivalent rxjs6 code should be like this:

from(list)
      .pipe(
        filter(a => a.name != null),
        groupBy(i => i.type),
        mergeMap((p) => {
          return p.pipe(
                    filter(f => f.selected),
                    count(),
                    mergeMap(c => {
                      return p.pipe(
                        count(),
                        map(totalCount => {
                          return {
                            key: p.key,
                            totalCount: totalCount,
                            selected: c
                          };
                        })
                      );
                    })
                );
        }),
        reduce((x, y) => {
          //please adjust your code here as i could not see isValid on x
          x.isValid = x.selectedCount > 0; 
          return x;
         })
      ).subscribe(r => {
        console.log(r + 'any item selected')
      }
      )

Hope it gives an idea of how to proceed.

user2216584
  • 5,387
  • 3
  • 22
  • 29
  • some how execution does not make it to p.pipe( map(totalCount => { return { key: p.key, totalCount: totalCount, selected: c }; }) – d-man Jun 26 '19 at 14:17
  • can you help me understand where does total count came from ? – d-man Jun 26 '19 at 14:18
  • @d-man Ah! I forgot to use `count` operator. I have edited my answer. Now you should get from where totalCount is coming. Hope it helps. – user2216584 Jun 26 '19 at 16:18