我觉得这很难解释,但我从我的通用数据库服务传递observables到我的组件。正在过滤的可观察对象的数组TypeScript
的方法是这样的:
getCollectionThroughDivisionId(id: UUID): Observable<T[]> {
return this._table.watch().map(returnedObjects =>
returnedObjects.filter(returnedObject => returnedObject.divisionId == id)
);
}
上述方法返回一个通用对象的可观察到的阵列,通过它divisionId过滤
的事情是我需要过滤内部的阵列的方法returnedObject为一个ID
getCollectionThroughUserId(id: UUID): Observable<T[]> {
return this._table.watch().map(returnedObjects =>
returnedObjects.filter(returnedObject => returnedObject.userId.filter(x => x == id))
);
}
参数userId是一个ID的数组,但默认返回整个集合(没有thr由于错误)。
编辑:
对象模型:
export class Task {
id: string;
name: string;
userId: string[];
projectId: string;
public constructor(init?: Partial<ITask>) {
Object.assign(this, init);
}
}
Json的例子:
{
"id": "TA18XVBJ3584ZM3" ,
"name": "User documentation & technical documentation" ,
"projectId": "PRYH3C72C6CQKZS" ,
"userId": [
"USGC6W4RCERY2ET",
"USKWQYHKB8E3X2U"
]
}
的目标是:遍历每个任务,检查用户id数组,如果它包含了通过用户id
过滤器可能不是正确的方法在这里使用,您应该使用地图,而不是为了将returnedObjects映射到具有较少用户ID的对象。 :) – toskv
我不太明白你的意思,我需要检查每个对象中的每个userId。你有可能给我一个例子吗? – Laurens
只是为了得到这个。你想要做的是从returnedObject.userId属性中过滤出用户ID。如果你有{userId:[1,2]}和id = 1,你应该找回{userId:[1]}? – toskv