如何解决从筛选可观察数组中获得第一个值
假设我有一个具有如下结构的可观察数组:
let arr = Observable.of([{a: null},{a: [1,2,3,4]},3]},{a: null}])
我想提取第一个对象,其中a
不是null
。如何退回{a: [1,4]}
?
解决方法
您可以尝试这样的事情
let arr = Observable.of([{a: null},{a: [1,2,3,4]},3]},{a: null}])
arr
.pipe(
// with this map you transform the array emitted by the source into the first item where a is not null
map(a => a.find(item => !!item.a)) // find is the array method
)
.subscribe(
// the data emitted here can be null if all items in the original array have an a property null
data => console.log("I am the first item with a not null,if any",data)
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。