0

Is there any way to use a map function in RxJs from the observable's emitted value's property?

Let's say I have an Observable of type: Observable<Something>, and Something has a property called mapFunction with the value: mapTo({ isTicking: true }). Is there anyway I can use this map function on that observable? Like this:

private composingObs$ = this.streamOfSomethings$.pipe( // I would like to use the instance function: something.mapFunction as map here.

4
  • Are you trying to map a function over the observables from streamOfSomethings? Commented Jan 2, 2020 at 3:02
  • Yes, the problem is that the mapping function is in a property of the "something" instance, which is not available until after streamOfSomethings$ been mapped. Commented Jan 2, 2020 at 3:26
  • Can you show us more code..? I can't really understand this Commented Jan 2, 2020 at 6:12
  • Could you post your code snippet for a clear understanding? @user10103655 Commented Jan 4, 2020 at 10:57

1 Answer 1

1

This will probably work. It fetch the function once, then subsequent emission will be handled by something.someFn which according to you is an operator mapTo

this.streamOfSomethings$.pipe(
take(1),
mergeMap(something=>this.streamOfSomethings$.pipe(something.someFn))
)

Although i think you should use general function or observable as property other than pipable functions, that will give you more flexibilities.

you can setup somethingObject as {data:..., fn:...}

somethingObject.pipe(map(({data,fn)=>fn(data)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.