Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

javascript - 过滤后比较数组中的父字段和子字段的值(Compare parent and child fields' values in array after filtering)

I have the following array of parents and children Stackblitz :

(我有以下parentschildren Stackblitz阵列:)

const parents = of(
  [
    { id: 1, type: 'A', children: [{ id: 3, type: 'A' }, { id: 4, type: 'C' }] },
    { id: 2, type: 'B', children: [{ id: 5, type: 'D' }, { id: 6, type: 'B' }] }
  ]);

I need to filter the parent where there is a child with id equal to 6:

(我需要过滤有id等于6的childparent :)

{ id: 2, type: 'B', children: [{ id: 5, type: 'D' }, { id: 6, type: 'B' }] }

For that I used RxJs operators:

(为此,我使用了RxJs运算符:)

parents.pipe(
  flatMap(parents => parents),
  map(parent => parent.children.find(child => child.id == 6))
).subscribe(x => console.log(x));

The output is the following child :

(输出是以下child :)

{id: 6, type: "B"}

But I need to get an Observable<boolean> which value is true if:

(但是我需要获得一个Observable<boolean> ,如果满足以下条件,则该值为true)

parent.type == child.type (In this case it would be true, e.g., 'B' == 'B')

I have been trying FlatMap , Reduce , Filter , Find , ...

(我一直在尝试FlatMapReduceFilterFind ,...)

But I am never able to end up with the parent so I can compare parent and child types.

(但是我永远无法以parent为最终对象,因此我可以比较parentchild类型。)

{ id: 2, type: 'B', child: { id: 6, type: 'B' } }
  ask by Miguel Moura translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you replace this: parent.children.find(child => child.id == 6) with parent.find(p => p.children.some(c => c.id === 6)) then instead of receiving {id: 6, type: "B"} you will receiver the parent with all children.

(如果您将其替换为: parent.find(p => p.children.some(c => c.id === 6))替换为parent.children.find(child => child.id == 6) ,而不是收到{id: 6, type: "B"}您将收到所有孩子的父母。)

If you want to set only the child that has id 6 you can edit the line map(parent => parent.children.find(child => child.id == 6)) with

(如果您只想设置ID为6的子项,则可以使用以下方法编辑线map(parent => parent.children.find(child => child.id == 6)))

 map(parent => {
   parent.child = parent.children.find(c => c.id === 6);
   return parent;
 })

After that instead of receiving {id: 6, type: "B"} you will get the parent with additional property child (parent.child) containing the child that has id 6. And then you can make the check parent.type === parent.child.type.

(之后,您将获得带有附加属性child(parent.child)的附加属性child(parent.child),而不是接收{id: 6, type: "B"} ,该属性包含具有ID 6的孩子。然后,您可以进行检查parent.type == = parent.child.type。)

Note that parent.child might be undefined so adding additional check would be necessary.

(请注意,parent.child可能未定义,因此有必要添加其他检查。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...