gpt4 book ai didi

javascript - 以 rxjs 方式删除对象的一部分

转载 作者:行者123 更新时间:2023-12-02 02:49:46 25 4
gpt4 key购买 nike

我有以下对象:

const myObject = {
items:[
{
name: 'John',
age: 35,
children: [
{
child: 'Eric',
age: 10,
sex: 'M'
},
{
child: 'Andrea',
age: 4,
sex: 'F'
}
]
},
{
name: 'Bob',
age: 23,
children: [
{
child: 'Oscar',
age: 1,
sex: 'M'
}
]
}
]
}

我通过添加以下内容来过滤结果:

const source = of(myObject).pipe(
map(x => x.items),
map(x => {
return x.filter(y => {
return y.children.find(y => y.sex === 'M');
})
})
);

source.subscribe(x => console.log(x));

按性别过滤确实有效,但我想从 json 中删除女孩。在这种情况下,Andrea 应该从对象中删除。

也许我缺少有关我可以使用的另一个运算符的知识?

最佳答案

你也需要过滤它。

const source = of(myObject).pipe(
map(x => x.items),
map(x => {
return x.filter(y => {
return y.children.some(y => y.sex === 'M');
});
}),
map(x => {
return x.map(y => {
return {
...y,
children: y.children.filter(c => c.sex === 'M');
};
});
}),
);

source.subscribe(x => console.log(x));

关于javascript - 以 rxjs 方式删除对象的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62195084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com