I would like to create a drop-down list and retrieve the elements from a webService.
My problem is that I would like to avoid displaying duplicate items because I have 9999 items.
Here is the JSON file.
Do you think it is possible to do this? Because, I have no idea how to program this?
The display method is this:
JavaScript
x
16
16
1
private getDropDownList(): void {
2
this.service.getDropDownList().pipe(
3
takeUntil(this.unsubscribe$)
4
).subscribe(res => {
5
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
6
this.corporateLines = res.TITRE.map(
7
corporateLine => {
8
return {
9
placelabel: corporateLine.PLACELABEL,
10
}
11
}
12
);
13
}
14
});
15
}
16
corporate.component.ts
JavaScript
1
36
36
1
export class CorporateComponent implements OnInit, OnDestroy {
2
3
private unsubscribe$ = new Subject<void>();
4
5
corporateLines: Corporate[] = [];
6
7
constructor(private service: CorporateService, private router: Router) { }
8
9
ngOnInit(): void {
10
this.getDropDownList();
11
}
12
13
ngOnDestroy(): void {
14
this.unsubscribe$.next();
15
this.unsubscribe$.complete();
16
}
17
18
private getDropDownList(): void {
19
this.service.getDropDownList().pipe(
20
takeUntil(this.unsubscribe$)
21
).subscribe(res => {
22
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
23
this.corporateLines = res.TITRE.map(
24
corporateLine => {
25
return {
26
placelabel: corporateLine.PLACELABEL,
27
28
}
29
}
30
);
31
}
32
});
33
}
34
35
}
36
corporate.response.ts
JavaScript
1
8
1
import { ApiResponse } from "src/shared/types/api.response";
2
3
export interface CorporateResponse extends ApiResponse {
4
TITRE: {
5
PLACELABEL: string;
6
}[];
7
}
8
corporate.ts
JavaScript
1
4
1
export interface Corporate {
2
placelabel: string;
3
}
4
corporate.component.html
JavaScript
1
11
11
1
<div class="row row-cols-3 pt-3">
2
<div class="col text-end">
3
<label for="filterForMarkets" class="form-label">Label</label>
4
</div>
5
<div class="col-4">
6
<select>
7
<option *ngFor="let line of corporateLines">{{line.placelabel }}</option>
8
</select>
9
</div>
10
</div>
11
Thanks
Advertisement
Answer
I think you could use the distinct operator like this:
JavaScript
1
16
16
1
private getDropDownList(): void {
2
this.service.getDropDownList().pipe(distinct(),
3
takeUntil(this.unsubscribe$)
4
).subscribe(res => {
5
if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
6
this.corporateLines = res.TITRE.map(
7
corporateLine => {
8
return {
9
placelabel: corporateLine.PLACELABEL,
10
}
11
}
12
);
13
}
14
});
15
}
16