询问rxjs使用Obsevable distinct

时间:2017-09-22 13:43:13

标签: rxjs reactive

我想知道如何使用Observable。 我想要做的是重复删除。可以移动以下示例1,但我想要做的不是这种格式,而是在提前准备阵列时如何烹饪。

orgLayerDistinct(allList: LabelMasterExt[]) {
// Observable.of( allList ).distinct( );

// [sample 1] このサンプルは動くが好みの形式ではない。
// [sample 1] This sample works, but it's not a form of favorite.
Observable.of<Person>(
{ age: 4, name: 'Foo'},
{ age: 7, name: 'Bar'},
{ age: 5, name: 'Foo'},
{ age: 6, name: 'Foo'})
.distinct((p: Person) => p.name)
.subscribe(x => console.log(x));

// [sample 2 experimental] 配列を用意してある前提で利用したい。
// [sample 2 experimental] I would like to use an array on the assumption that it is prepared.
const persons: Person[] = [];
persons.push({ age: 4, name: 'Foo'});
persons.push({ age: 7, name: 'Bar'});
persons.push({ age: 5, name: 'Foo'});
persons.push({ age: 6, name: 'Foo'});
Observable.of<Person[]>(persons)
.distinct((p: Person) => p.name)
.subscribe(x => console.log(x));

}

[样本2实验] 但是,这会产生以下错误。

The type argument for type parameter 'T' cannot be inferred from the usage.
Consider specifying the type arguments explicitly.
Type argument candidate 'Person[]' is not a valid type argument
because it is not a supertype of candidate 'Person'.
Property 'includes' is missing in type 'Person'.

有什么好的计划吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Observable.from<Person>(array)Observable.of<Person>(...array)

第二个示例的问题是Observable.of<Person[]>()个元素是Person的数组,但.distinct()期望输入Person类型。