前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rxjs 里 filter(Boolean) 的用法

Rxjs 里 filter(Boolean) 的用法

作者头像
Jerry Wang
发布2021-07-21 09:38:38
4430
发布2021-07-21 09:38:38
举报

StackOverflow 上的讨论:

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

一个例子:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
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));

其实,这种写法等价于:

代码语言:javascript
复制
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.

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-06-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档