Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to filter duplicate strings in an array

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" class="form-check">
    <input  (change)="filterMarcas($event)" class="form-check-input" type="radio"  name="marca" value={{categ.marcaJ.marca}} id="flexCheckChecked" >
    <label class="form-check-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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading