欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Rxjs 里 filter(Boolean) 的用法

发布时间:2023/12/19 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Rxjs 里 filter(Boolean) 的用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

StackOverflow 上的讨论:

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

一个例子:

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 trueconsole.log((new Boolean("truthy")) === true); // prints falseconsole.log(Boolean("truthy") === true); // prints true

总结

以上是生活随笔为你收集整理的Rxjs 里 filter(Boolean) 的用法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。