Skip to content

Instantly share code, notes, and snippets.

@argyleink
Created January 6, 2017 22:14
Show Gist options
  • Save argyleink/ad950658d173ca37064fecc6b1d43a68 to your computer and use it in GitHub Desktop.
Save argyleink/ad950658d173ca37064fecc6b1d43a68 to your computer and use it in GitHub Desktop.
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
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