I have the following array of parents
and children
Stackblitz :
(我有以下parents
和children
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的child
的parent
:)
{ 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
, ...
(我一直在尝试FlatMap
, Reduce
, Filter
, Find
,...)
But I am never able to end up with the parent
so I can compare parent
and child
types.
(但是我永远无法以parent
为最终对象,因此我可以比较parent
和child
类型。)
{ id: 2, type: 'B', child: { id: 6, type: 'B' } }
ask by Miguel Moura translate from so