Let's say I have an observable array with structure such as:
let arr = Observable.of([{a: null}, {a: [1,2,3,4]}, {a: [1,2,3]}, {a: null}])
And I want to pull out the first object where a
is not null
. How can I return {a: [1,2,3,4]}
?
You can try something like this
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)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.