Context
I have a list of services that I am extracting in an array, but there is data that is repeated as shown in the image:
And I have the following code to filter the array to get the list of services:
// Request that obtains the services and maps the object to extract the array
getGroupService() {
this.bookingService.getDoctorById("5d24476eea8ebc659106657c").pipe(takeUntil(this.unsubscribe$))
.subscribe((res: profileAffiliate) => {
this.virtual = res.affiliate.groupedServices.virtual.map(service => service.service.name.es)
this.encasa = res.affiliate.groupedServices.atHome.map(service => service.service.name.es)
this.clinica = res.affiliate.groupedServices.clinic.map(service => service.service.name.es)
//console.log('clinica', this.clinica);
console.log(this.virtual, this.encasa, this.clinica);
})
}
// Method that opens the modal and checks if the array where the groupedServices array is stored // is empty
openServiceModal() {
if (this.groupedServices.length > 0) {
const dialogRef = this.dialog.open(SelectServiceModalComponent, {
data: { services: this.groupedServices, selected: this.services.value ?? [] },
panelClass: 'DialogContainer'
});
dialogRef.afterClosed().subscribe(selectedServices => {
console.log('The dialog was closed', selectedServices);
if (selectedServices) {
this.services.setValue(selectedServices)
}
});
}
else {
this.openSnackBar();
}
}
// Modal, where I filter (search) the services
selectedServices: any[] = [];
//array donde almaceno el elemento
filteredServices: any[] = [];
services: any[] = [];
ngOnInit(): void {
this.filteredServices = this.services;
this.search.valueChanges.pipe(takeUntil(this.unsubscribe$)).subscribe((val: string) => {
console.log(val);
if (val == "") {
this.filteredServices = this.services;
}
else {
}
this.filteredServices = this.services.filter((x: string) => {
return x.toLowerCase().includes(val.toLowerCase());
});
console.log('selectedServices :>> ', this.selectedServices);
console.log('filteredServices :>> ', this.filteredServices);
console.log('services :>> ', this.services);
})
}
The json response when filtering the data, who filters and obtains the array is this.services = data.services;. I also attach the data model https://github.com/IzliaB/json
How can I remove duplicate data?
You can use
set()
to get only the non-duplicated data.It occurs to me simply by making a loop in which you begin to discriminate the identical elements
I provide you with a very simple script that does the task you are looking for, the idea would be that you only adapt it
Greetings and God bless you