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

Dea*_*lla 1 javascript rxjs typescript angular

我有以下对象:

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'
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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));
Run Code Online (Sandbox Code Playgroud)

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

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

sat*_*ime 5

你也需要过滤它。

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));
Run Code Online (Sandbox Code Playgroud)