Rxjs 里 filter(Boolean) 的用法

* 上的讨论:

https://*.com/questions/53953015/using-rxjs-with-filterboolean-for-queries

一个例子:
Rxjs 里 filter(Boolean) 的用法

const result$ = events.get(CartAddEntrySuccessEvent).pipe(
      // When the above event is captured, wait for the cart to be stable
      // (because OCC reloads the cart after any cart operation)...
      switchMap((event) =>
        cartService.isStable().pipe(filter(Boolean), mapTo(event))
      ),
      // Merge the state snapshot of the cart with the data from the event:
      withLatestFrom(cartService.getActive()),
      map(([event, cart]) => ({ ...event, cart }))
    );

    result$.subscribe((data) => console.log('Jerry', data));

其实,这种写法等价于:

filter(v=>!!v)

The Boolean global reference points to the constructor function which returns a boolean value from the first argument.

The constructor can be used to create a boolean wrapper object, but it is not the same as the primitive true value.

console.log(new Boolean("truthy")); // prints an object.
    console.log(new Boolean("truthy").valueOf() === true); // prints true
    console.log((new Boolean("truthy")) === true); // prints false
    console.log(Boolean("truthy") === true); // prints true

 

上一篇:从零开始搭建微信硬件开发环境全过程——1小时掌握微信硬件开发流程


下一篇:蓝牙Ibeacon室内定位和微信摇一摇周边原理分析