Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
There an easier way? RXJS subject to increment likes on an array object
// this really what it takes to increment a counter on an item in my observable
// likes is a subject, broadcasting the updated array eventually to the source observable being rendered
this.likes
.map(
(message: Message): IMessagesOperation =>
(messages: Message[]) => {
return messages.map((m: Message) => {
// todo: check if user has already liked
if (m.id == message.id)
++m.likes
return m
})
}
)
.subscribe(this.updates)
// help me find a better way please =)
@oliamb

This comment has been minimized.

Copy link

oliamb commented May 31, 2017

Did you think about using .reduce() operator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.