RxJS在属性中的对象数组上不同

问题描述 投票:0回答:1

我有一个可观察到的东西,返回的东西是这样的:

"@odata.context": "here is the context URL",
"value": [
        {
            "Id": 1,
            "Value": "A"
        },
        {
            "Id": 2,
            "Value": "B"
        },
        {
            "Id": 3,
            "Value": "C"
        },
        {
            "Id": 4,
            "Value": "A"
        }
    ]
}

使用RxJS,我想进入“值”属性,并在其上使用distinct,以将响应限制为如下所示:

"@odata.context": "here is the context URL",
"value": [
        {
            "Id": 1,
            "Value": "A"
        },
        {
            "Id": 2,
            "Value": "B"
        },
        {
            "Id": 3,
            "Value": "C"
        }
    ]
}

您能帮我吗?

rxjs
1个回答
1
投票

在很大程度上取决于您要使用此RxJS链的精确程度,但是我希望这会为您指明正确的方向:

const source = of(data)
  .pipe(
    mergeMap(d => d.value),
    distinct(v => v.Value),
    toArray(),
  )
  .subscribe(console.log);

mergeMap将重申值数组作为单独的发射,其中distinct将忽略重复的Value。然后toArray()只会收集所有结果。

实时演示:https://stackblitz.com/edit/rxjs-is1zp4

© www.soinside.com 2019 - 2024. All rights reserved.