I have this structure on my angular project:
export interface Campo {
id?: number;
nome?: string;
tabela?: Tabela;
campos?: Campo[];
}
export interface Tabela {
id?: number;
name?: string
arquivo?: Arquivo;
}
export interface Arquivo {
id?: number;
name?: string;
tabelas?: Tabela[];
}
i.e., An "Arquivo" has many "Tabela", and "Tabela" has many "Campo". I subscribe on a service that return all "Campo" and I keep it on "campos" variable. This service return more than 200 "Campos", associated with about 10 "Tabelas" and only 3 "Arquivos".
I want to return all unique "Arquivo" (i.e. all 3 "Arquivo").
When I do:
let arquivos = this.campos
.map(campo => campo.tabela.arquivo.id)
.filter((value, index, self) => self.indexOf(value) === index);
It returns
[1001, 1002, 1003]
So it is the ids of all 3 "Arquivo" that I have. But I dont want the ids, I want the "Arquivo" objects. When I try:
let arquivos = this.campos
.map(campo => campo.tabela.arquivo)
.filter((value, index, self) => self.indexOf(value) === index);
It returns more than 200 registers, because I have more than 200 "Campos" and it can't know what "Arquivos" is unique or not.
How can I do that?