[英]RXJS filter on another observable
In my code I am getting a list of users from MS Graph , but this lists all users. 在我的代码中,我从MS Graph获取了一个用户列表,但这列出了所有用户。 I want to emit only those users that are a member of a specified group.
我只想发出属于指定组成员的那些用户。 So I have set up a filter:
所以我设置了一个过滤器:
private loadUsers() {
console.log(`Loading users...`);
this.graph.get$(`users`)
.map(resp => resp.json().value)
.flatMap(user => user)
.do(user => {
console.log(`Got user: ${user['displayName']}`);
})
.filter(user => {
// we only want users who are members of the TrackerContact group
const userId: string = user['id'];
console.log(`Checking user ${userId}`);
return this.userIsTrackerContact(user); // <<= PROBLEM HERE!
})
.subscribe(_ => {
console.log(`Completed user fetch!`);
});
}
The problem is in order to find out what groups a user is a member of, I must make another REST call back to MS Graph : 问题是为了找出用户所属的组,我必须对MS Graph进行另一个REST调用:
private userIsTrackerContact(user): Observable<boolean> {
// get the groups this user is a member of
return this.graph.get$(`users/${user['id']}/memberOf`)
.map(resp => resp.json().value)
.do(groups => {
console.log(`${user['displayName']} is a member of ${groups.length} groups:`);
groups.forEach((group, idx) => {
console.log(` [${idx+1}]: ${group['displayName']}`);
})
})
.map(groups => {
const found = groups.find((group) => group['id'] === this.authConfig.azureGroups.trackerContact)
return !isUndefined(found);
})
}
It does not seem to me that it's possible to filter an Observable based on another observable. 在我看来,不可能根据另一个可观察对象过滤一个可观察对象。 The predicate test must be a simple true/false test.
谓词测试必须是简单的正确/错误测试。
How can I filter an Observable based on the results of another Observable? 如何根据另一个Observable的结果过滤Observable?
.concatAll() might be better: .concatAll()可能更好:
this.graph.get$(`users`)
.map(resp => resp.json().value)
.flatMap(user => user)
.do(user => {
console.log(`Got user: ${user['displayName']}`);
})
.filter(user => {
// we only want users who are members of the TrackerContact group
const userId: string = user['id'];
console.log(`Checking user ${userId}`);
return this.userIsTrackerContact(user); // <<= PROBLEM HERE!
})
.concatAll()
.subscribe(...)
I believe you should be able to merge the async observables then filter. 我相信您应该能够合并异步可观察对象然后进行过滤。
Rx.Observable.merge(this.graph.get$(`users`)
.map(resp => resp.json().value)
.flatMap(user => user)
.do(user => {
console.log(`Got user: ${user['displayName']}`);
})
.map(user => {
// we only want users who are members of the TrackerContact group
const userId: string = user['id'];
console.log(`Checking user ${userId}`);
return this.userIsTrackerContact(user); // <<= PROBLEM HERE!
}))
.filter(x => x)
.subscribe(_ => console.info('success'));
You can change your predicate set a property on user isGroupMember. 您可以更改谓词,在用户isGroupMember上设置属性。 Then
filter(x => x.isGroupMember)
. 然后
filter(x => x.isGroupMember)
。 Then you have the user objects in the subscribe method. 然后,将用户对象包含在subscribe方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.