I have a response from an API that comes in this style
"0": {
"id": 78,
"fotos": ["78.0", null, null, null, null, null],
"status": "A",
"marcaJ": { "id": 12, "marca": "ROYAL CANIN" },
"item_linkWeb": {
"id": 98,
"emp_id": 1,
"item_id": 78,
"linkWeb_id": 72,
"updated_at": "2021-10-07T18:35:14.000000Z",
"ecommerce_id": 1662526,
"coresJ": [
{ "ecommerce_id": null, "cor": null, "hex": null },
{ "ecommerce_id": null, "cor": null, "hex": null }
],
"atributosJ": {
"Peso": { "valor": "null" },
"Tamanho": { "valor": "null" }
},
"fotosJ": [6354862],
"linkWeb": {
"id": 72,
"titulo": "Ra\u00e7\u00e3o Royal Canin Mini Adulto 2,5 kg"
}
},
"estoque": null,
"tabela": {
"itemTabela_id": 4,
"item_id": 78,
"vVenda": 136.9,
"vPromo": null,
"vPromoData": null
}
},
"1": {
"id": 209,
"fotos": ["209.0", null, null, null, null, null],
"status": "A",
"marcaJ": { "id": 12, "marca": "ROYAL CANIN" },
"item_linkWeb": {
"id": 114,
"emp_id": 1,
"item_id": 209,
"linkWeb_id": 88,
"updated_at": "2022-03-28T15:02:28.000000Z",
"ecommerce_id": 1662563,
"coresJ": [
{ "ecommerce_id": null, "cor": null, "hex": null },
{ "ecommerce_id": null, "cor": null, "hex": null }
],
"atributosJ": {
"Peso": { "valor": "2,5 kg" },
"Tamanho": { "valor": "null" }
},
"fotosJ": [6355016],
"linkWeb": {
"id": 88,
"titulo": "Ra\u00e7\u00e3o Royal Canin Mini Indoor Junior 2,5 kg"
}
},
"estoque": { "item_id": 209, "estAtual": 3 },
"tabela": {
"itemTabela_id": 4,
"item_id": 209,
"vVenda": 159.9,
"vPromo": null,
"vPromoData": null
}
},
I tried to make a filter of when marcaJ.marca
is repeated returns only one value
<div *ngFor="let categ of filtro" >
<input (change)="filterMarcas($event)" type="radio" name="marca" value={{categ.marcaJ.marca}} id="flexCheckChecked" >
<label for="flexCheckChecked">
{{categ.marcaJ.marca}}
</label>
</div>
this.filtro.push(resB[i]);
this.filtro = this.filtro.filter( (ele,pos)=>this.filtro.indexOf(ele) == pos);
but did not return the expected value, it seems that I am not able to access themarcaJ.marca in the filter
CodePudding user response:
Are you looking to turn that array from 2 entries into one? You can do something like this:
const result = this.filtro.filter((record, index) => temp.findIndex(check => check.marcaJ.marca === record.marcaJ.marca) === index);
This finds the first occurrence of each marca
CodePudding user response:
This will remove the duplicates.
const res = {
0: {
id: 78,
fotos: ['78.0', null, null, null, null, null],
status: 'A',
marcaJ: { id: 12, marca: 'ROYAL CANIN' },
item_linkWeb: {
id: 98,
emp_id: 1,
item_id: 78,
linkWeb_id: 72,
updated_at: '2021-10-07T18:35:14.000000Z',
ecommerce_id: 1662526,
coresJ: [
{ ecommerce_id: null, cor: null, hex: null },
{ ecommerce_id: null, cor: null, hex: null },
],
atributosJ: {
Peso: { valor: 'null' },
Tamanho: { valor: 'null' },
},
fotosJ: [6354862],
linkWeb: {
id: 72,
titulo: 'Ra\u00e7\u00e3o Royal Canin Mini Adulto 2,5 kg',
},
},
estoque: null,
tabela: {
itemTabela_id: 4,
item_id: 78,
vVenda: 136.9,
vPromo: null,
vPromoData: null,
},
},
1: {
id: 209,
fotos: ['209.0', null, null, null, null, null],
status: 'A',
marcaJ: { id: 12, marca: 'ROYAL CANIN' },
item_linkWeb: {
id: 114,
emp_id: 1,
item_id: 209,
linkWeb_id: 88,
updated_at: '2022-03-28T15:02:28.000000Z',
ecommerce_id: 1662563,
coresJ: [
{ ecommerce_id: null, cor: null, hex: null },
{ ecommerce_id: null, cor: null, hex: null },
],
atributosJ: {
Peso: { valor: '2,5 kg' },
Tamanho: { valor: 'null' },
},
fotosJ: [6355016],
linkWeb: {
id: 88,
titulo: 'Ra\u00e7\u00e3o Royal Canin Mini Indoor Junior 2,5 kg',
},
},
estoque: { item_id: 209, estAtual: 3 },
tabela: {
itemTabela_id: 4,
item_id: 209,
vVenda: 159.9,
vPromo: null,
vPromoData: null,
},
},
3: {
id: 209,
fotos: ['209.0', null, null, null, null, null],
status: 'A',
marcaJ: { id: 14, marca: 'SOME_BRAND' },
item_linkWeb: {
id: 114,
emp_id: 1,
item_id: 209,
linkWeb_id: 88,
updated_at: '2022-03-28T15:02:28.000000Z',
ecommerce_id: 1662563,
coresJ: [
{ ecommerce_id: null, cor: null, hex: null },
{ ecommerce_id: null, cor: null, hex: null },
],
atributosJ: {
Peso: { valor: '2,5 kg' },
Tamanho: { valor: 'null' },
},
fotosJ: [6355016],
linkWeb: {
id: 88,
titulo: 'Ra\u00e7\u00e3o Royal Canin Mini Indoor Junior 2,5 kg',
},
},
estoque: { item_id: 209, estAtual: 3 },
tabela: {
itemTabela_id: 4,
item_id: 209,
vVenda: 159.9,
vPromo: null,
vPromoData: null,
},
},
};
const filterDuplicates = (res) => {
return Object.values(res).filter((target, i, self) => {
return i === self.findIndex(({ marcaJ }) => target.marcaJ.id === marcaJ.id);
});
};
console.log(filterDuplicates(res));