我想基于路由参数,以获得特定项目,例如,{ key: "321" }
,从ngrx/store
。我得到它的工作是这样的:拼合可观察到的
this.route.params
.map((params: any) => params.key)
.mergeMap((key: string) => {
return this.store.select(state => state.items)
.mergeMap((items: Observable<any[]>) => items)
// .do(item => console.log(item, key))
.filter((item: any) => item.key === key);
})
.take(1)
.subscribe(console.log);
其中state.items
为对象的数组,如:[ {key: "1"}, {key: "2"}, ...]
填充随着时间的推移。
我想知道有没有更好/不同的方式来做到这一点?
此外,为什么我会在.take(1)
之前获得相同的项目倍数(state.items.length
)倍?
你想从你们店里的物品数组提取多个项目?如果是这样的话,你的代码可以被优化。 – KwintenP
@KwintenP我需要通过特定属性的值从列表中获取项目。例如,对于url“/ items/123”,值为** 123 **,属性为**键**。 – Sasxa