Angular 应用筛选器后,使用父类和子类定义条件

Angular 应用筛选器后,使用父类和子类定义条件,angular,typescript,rxjs,angular8,Angular,Typescript,Rxjs,Angular8,在角度8中,我有一个可观测的: let parents: Observable<Parent[]>; 我需要在所有父母的孩子中找到id值为1的孩子 然后我需要定义一个与该子对象及其父对象相关的条件。。。我试过: parents.pipe( map(parents => parents.map(parent => parent.children)), map(children => children.find(child => child.

在角度8中,我有一个可观测的:

let parents: Observable<Parent[]>;
我需要在所有父母的孩子中找到id值为1的孩子

然后我需要定义一个与该子对象及其父对象相关的条件。。。我试过:

  parents.pipe(
    map(parents => parents.map(parent => parent.children)),
    map(children => children.find(child => child.id == 1))
    map(child => Test Condition with resulting Child and its Parent 
我遇到的第一个问题是mapchildren=>中的子项的类型是Child[]],而不是Child[],因此表达式Child.id失败,因为Child是Child[]

在最后一个映射中,我需要有子对象,但也需要有父对象,这样我就可以创建条件并返回一个可观察的对象

我该怎么做?无法使用map完成吗?

请尝试以下操作:

parents.pipe(
    map(parents => parents.map(parent => parent.children).flat()),
    map(children => children.find(child => child.id == 1))
    map(child => Test Condition with resulting Child and its Parent 

你需要使用平面图。 使用flatMap,您可以将所有子列表连接到一个长列表中,以便在接下来的步骤中处理

我认为这应该奏效:

const childrenWithId1: Children[] = parents.pipe(
    flatMap(parents => parents),
    flatMap(parent => parent.children),
    filter(child => child.id == 1))

将三个映射替换为一个映射,这将调用一个返回所需内容的函数。此外,children是复数形式。子项的单数为child.mapparents=>parents.mapparent=>parent.childrens返回数组数组。Concat您的结果并寻找一个特定的子项。这里有一个关于flat的简单示例:[[1,2],[3,4]]。flat。你只需要做同样的把戏。或者concat也可以这样做,但这是一个很长的构造:Array.prototype.concat.apply[],[[1,2],[3,4]]。我以前尝试过,但在flatMap中,父对象的类型是parent[],因此父对象没有属性子对象。然后首先是flatMap父对象。。。flatMapparents=>Parents我更新了原始响应中截取的代码,解决了部分问题。。。我知道只有一个孩子的身份证是1。然后我需要创建一个与该子对象及其父对象相关的条件。。。因此,得到一个孩子不允许我这样做。我需要返回一个布尔值的可观察值,这是条件的结果。你的代码给我的错误属性“flat”在类型“Child[][]上不存在。第一个父级应该像parentsStream一样调用,因为它是可观察的,那么你在第一个映射中有一个父级数组。你使用IE或Edge吗?不。。。我正在使用Safari。但是我得到的错误是在我的JS编辑器中。
const childrenWithId1: Children[] = parents.pipe(
    flatMap(parents => parents),
    flatMap(parent => parent.children),
    filter(child => child.id == 1))