属性中对象数组上的RxJS distinct
我有一个可观测的,返回如下的东西:属性中对象数组上的RxJS distinct,rxjs,Rxjs,我有一个可观测的,返回如下的东西: "@odata.context": "here is the context URL", "value": [ { "Id": 1, "Value": "A" }, { "Id": 2, "Value": "B" }, { "Id": 3,
"@odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
},
{
"Id": 4,
"Value": "A"
}
]
}
"@odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
}
]
}
使用RxJS,我想进入“value”属性,并在其上使用distinct来限制对以下内容的响应:
"@odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
},
{
"Id": 4,
"Value": "A"
}
]
}
"@odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
}
]
}
你能帮我一下吗?这主要取决于你到底想如何使用这条RxJS链,但我希望这能指引你正确的方向:
const source = of(data)
.pipe(
mergeMap(d => d.value),
distinct(v => v.Value),
toArray(),
)
.subscribe(console.log);
mergeMap
将重复值数组作为单独的排放,其中distinct
将忽略重复的Value
s。然后toArray()
将只收集所有结果
现场演示: