i'm trying to understand functional programming with rx for js.
i have an Rx.Observable that emits "post" objects:
every post looks like this :
{
title: "sometitle",
author: "someauthor"
text: "sometext",
date: "somedate",
tags: ['tag1', 'tag2', ..., 'tagN']
}
and i want to transform that sequence into a sequence that emits:
{
tag: 'tagname',
postCount: n
}
This is what i have so far:
function tags(post) {
return post
.tags
.map(function(tag) { return { 'tag': tag, 'count': 1});
}
posts
.flatMap(tags)
.groupBy(function(tagged) { return tagged.tag })
. // don't know how to continue
as i said before, my goal is to create a sequence/observable that emits {tag: 'tagname', postCount: n } for each tag
thx in advance
edit:
i forgot to mention that i was looking for a "node oriented" answer.
this is what i have so far. it works, but i'm not sure about the { ..., count: 1 } part.
i'm looking for a more "elegant" solution.
posts
.flatMap(tags)
.map((tag) => {return {name: tag, count: 1}})
.groupBy((tagcount) => {return tagcount.name})
.flatMap((taggroup) => {return taggroup.reduce((a,x) => {return {tag: x.name, count: (a.count + x.count)}})})