Rxjs 在数组 属性 上使用最小运算符
Rxjs use min operator on array property
我知道我们可以对数字数组使用最小运算符。但是我如何在对象数组上使用它,数字为 属性?
var source = Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8]).min();
文档是这样描述的。这将发出 1
。我该怎么做...
var source = Rx.Observable.fromArray([{a: 1, b:"first"},{a: 3, b:"second"},{a: 5, b:"third"}]).min();
我想通过内部 属性 值使用 min
。因此,比较数组中每个对象中 a
的值并发出该对象。
我知道 min 有一个比较功能,但我不知道它是否可以在这里使用。
我还希望它发出对象,而不是最小 属性 值。
编辑:
因此,我在 Angular 4 应用程序中使用 Ngrx,并尝试在 store.select
Observable 上使用 min
运算符。出于某种原因,下面的代码,我首先订阅然后从响应创建另一个可观察对象,然后使用 min 工作,但是如果我跳过订阅并尝试像下面的代码一样使用 min 运算符,它会失败并发出完整的 ClientFacilities 数组。
有人知道这是怎么回事吗?两者不是一回事吗?
这个有效:
this.store.select(fromRoot.getClientFacilitiesArray).take(1).subscribe(res => {
Observable.from(res).min<ClientFacility>((a, b) => a.leaders_assigned - b.leaders_assigned)
.subscribe(res => console.log(res));
})
这失败了:
this.store.select(fromRoot.getClientFacilitiesArray).take(1)
.min<ClientFacility>((a, b) => a.leaders_assigned - b.leaders_assigned)
.subscribe(res => console.log(res));
Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8]).min()
是 RxJS 4 的示例,它不适用于 RxJS 5。
RxJS 5 文档的正确位置是 http://reactivex.io/rxjs/ , while http://reactivex.io/documentation/ 包含 RxJS 4 文档。
根据min
operator documentation, it can accept compare function, which behaves similarly the one accepted by array sort
method。
Here是一个例子:
Rx.Observable.from([{a: 1, b:"first"},{a: 3, b:"second"},{a: 5, b:"third"}])
.min((objA, objB) => objA.a - objB.a)
.subscribe(val => console.log(val));
我知道我们可以对数字数组使用最小运算符。但是我如何在对象数组上使用它,数字为 属性?
var source = Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8]).min();
文档是这样描述的。这将发出 1
。我该怎么做...
var source = Rx.Observable.fromArray([{a: 1, b:"first"},{a: 3, b:"second"},{a: 5, b:"third"}]).min();
我想通过内部 属性 值使用 min
。因此,比较数组中每个对象中 a
的值并发出该对象。
我知道 min 有一个比较功能,但我不知道它是否可以在这里使用。
我还希望它发出对象,而不是最小 属性 值。
编辑:
因此,我在 Angular 4 应用程序中使用 Ngrx,并尝试在 store.select
Observable 上使用 min
运算符。出于某种原因,下面的代码,我首先订阅然后从响应创建另一个可观察对象,然后使用 min 工作,但是如果我跳过订阅并尝试像下面的代码一样使用 min 运算符,它会失败并发出完整的 ClientFacilities 数组。
有人知道这是怎么回事吗?两者不是一回事吗?
这个有效:
this.store.select(fromRoot.getClientFacilitiesArray).take(1).subscribe(res => {
Observable.from(res).min<ClientFacility>((a, b) => a.leaders_assigned - b.leaders_assigned)
.subscribe(res => console.log(res));
})
这失败了:
this.store.select(fromRoot.getClientFacilitiesArray).take(1)
.min<ClientFacility>((a, b) => a.leaders_assigned - b.leaders_assigned)
.subscribe(res => console.log(res));
Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8]).min()
是 RxJS 4 的示例,它不适用于 RxJS 5。
RxJS 5 文档的正确位置是 http://reactivex.io/rxjs/ , while http://reactivex.io/documentation/ 包含 RxJS 4 文档。
根据min
operator documentation, it can accept compare function, which behaves similarly the one accepted by array sort
method。
Here是一个例子:
Rx.Observable.from([{a: 1, b:"first"},{a: 3, b:"second"},{a: 5, b:"third"}])
.min((objA, objB) => objA.a - objB.a)
.subscribe(val => console.log(val));