I'm having difficulty understanding a simple RxJs query that involves merging Observables that are wrapped inside an object. If I directly return the Observable from flatMap, it works fine but I also need to include the name in my output. How can I accomplish this?
This problem is specifically related to RxJS 5.0.0-beta.2
Here is the basic data structure:
var data = [
{
"name": "Test #1",
"users": [
{
"name": "John Doe",
"gender": "male"
},
{
"name": "John Doe",
"gender": "male"
},
{
"name": "Jane Doe",
"gender": "female"
}
]
},
{
"name": "Test #2",
"users": [
{
"name": "John Doe",
"gender": "male"
},
{
"name": "Jane Doe",
"gender": "female"
},
{
"name": "Jane Doe",
"gender": "female"
}
]
}
];
The RxJs function in question:
Observable.fromArray(data)
.flatMap(x => {
return Observable.of({
"name": x.name,
"count": Observable.fromArray(x.users)
.filter(x => x.gender == "male")
.count()
})
})
.toArray()
.subscribe(result => {
console.log(result);
});
The desired result should be:
result = [
{
"name": "Test #1",
"count": 2
},
{
"name": "Test #2",
"count": 1
}
];
However, the actual result obtained is:
result = [
{
"name": "Test #1",
"count": Observable
},
{
"name": "Test #2",
"count": Observable
}
];