[英]Get first value from filtering an observable array
假設我有一個 observable 數組,其結構如下:
let arr = Observable.of([{a: null}, {a: [1,2,3,4]}, {a: [1,2,3]}, {a: null}])
我想取出a
不為null
的第一個對象。 我怎樣才能返回{a: [1,2,3,4]}
?
你可以試試這樣的
let arr = Observable.of([{a: null}, {a: [1,2,3,4]}, {a: [1,2,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)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.