gpt4 book ai didi

javascript - 删除搜索结果/angular2/typescript/js 中的重复值

转载 作者:行者123 更新时间:2023-11-28 05:20:58 25 4
gpt4 key购买 nike

我需要删除搜索结果中的重复值。请帮我找到一个简单的解决方案。

我的buyer-search.service.ts:

export class BuyerSearchService {
constructor(private http: Http) {}
search(term: string, atrib: string): Observable<Buyer[]> {
return this.http
.get(`app/buyers/?${atrib}=${term}`)
.map((r: Response) => r.json().data as Buyer[]);
}
}

我的buyer-search.component.ts

export class BuyerSearchComponent implements OnInit {

buyers: Observable<Buyer[]>;
private searchTerms = new Subject<string>();
constructor(
private buyerSearchService: BuyerSearchService,
private router: Router) {}
// Push a search term into the observable stream.
search(term: string): void {
this.searchTerms.next(term);
}

ngOnInit(): void {
this.buyers = this.searchTerms
.debounceTime(300) // wait for 300ms pause in events
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time
// return the http search observable
? this.buyerSearchService.search(term, 'clientName')
// or the observable of empty buyers if no search term
: Observable.of<Buyer[]>([]))
.catch(error => {
// TODO: real error handling
console.log(error);
return Observable.of<Buyer[]>([]);
});
}
}

和 View ,buyer-search.component.html:

<div id="search-component">
<input #searchBox id="search-box" (keyup)="search(searchBox.value)"/>
<div id="buyerByNameResults">
<div *ngFor="let buyer of buyers | async"
class="search-result">
<p class="buyer-name-s">{{buyer.clientName}}</p>
</div>
</div>
</div>

enter image description here

最佳答案

也许您需要对内部可观察对象执行 .distinct() ,并将键选择器传递给第一个参数。

this.buyerSearchService
.search(term, 'clientName')
.distinct(function (x) { return x.clientName; });

还有第二个参数,允许您在需要时提供比较器。

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/distinct.md

关于javascript - 删除搜索结果/angular2/typescript/js 中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com