given an array of object typed interface AvvocatoParte { indirizzi: Indirizzo[];…
- User
given an array of object typed interface AvvocatoParte { indirizzi: Indirizzo[]; indirizzo?: Indirizzo; cassazionista?: boolean | null; tipoDifensore?: string; tipo?: TipoAvvocato; foroRif?: string; tipoParte?: string; avvocato: avvocato; } and an array of type interface DifensoreVerbale { id: DifensoreVerbaleID; idRicUdien: number; idAnagDifen: number; idParte: number; comparso: boolean; operatore: number; oggi: number; parte?: Parte; anagraficaAvvocato?: AnagraficaAvvocato; tipoDif: string; citta: string; via: string; }
return one array where it removes all duplicates of Difensore verbale, check if the id that i’ll give is equal to DifensoreVerbale.idParte, if is equal remove all the duplicates where DifensoreVerbale.idAnagDifen is equal to AvvocatoParte.avvocato.id using lodash functions
- Assistant
:
import _ from 'lodash'; const removeDuplicates = (avvocatiParte: AvvocatoParte[], difensoriVerbale: DifensoreVerbale[], id: number): DifensoreVerbale[] => { const difensoriFiltrati = _.filter(difensoriVerbale, d => d.idParte === id); // filtra i difensori verbali con idParte uguale a quello dato const idAvvocatiDifesa = _.map(_.filter(avvocatiParte, a => a.tipoDifensore === 'DIFENSORE' && a.avvocato && a.avvocato.id), a => a.avvocato.id); // recupera gli id degli avvocati difensori presenti nell'array avvocatiParte const difensoriSenzaDuplicati = _.uniqBy(difensoriFiltrati, d => d.idAnagDifen); // rimuove i difensori con stesso idAnagDifen const difensoriFiltratiPerAvvocati = _.filter(difensoriSenzaDuplicati, d => !idAvvocatiDifesa.includes(d.idAnagDifen)); // rimuove i difensori che sono anche avvocati difensori return difensoriFiltratiPerAvvocati; }Explanation:
-
First, we filter the
difensoriVerbalearray to get only the elements with theidParteequal to the givenid. -
Then, we map the filtered
avvocatiPartearray to get only theidof theavvocatothat are defense lawyers (tipoDifensoreequals 'DIFENSORE') and not null (avvocato.idexists). -
We use
_.uniqByto remove the duplicate elements from thedifensoriFiltratiarray, using theidAnagDifenproperty as the unique identifier. -
Finally, we filter the
difensoriSenzaDuplicatiarray to remove the elements that have the sameidAnagDifenas theavvocato.id.
Note: This solution assumes that you have access to the
lodashlibrary. -