Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
mat-select with grouping common options under a subheading, angular

I have an object of arrays that looks like this

 const books = {
    __typename: '',
    items: [
      {
         id:''
         store: 1
         type: 'ROMAN'
         name: 'blalala'
      }
      {
         id:''
         store: 1
         type: 'BIOGRAPHY'
         name: 'blalala'
      }
      {
         store: 1
         id:''
         type: 'ROMAN'
         name: 'blalala'
      }
      {
         id:''
         store: 2
         type: 'ROMAN'
         name: 'blalala'
      }
]

What I need is to have a mat select containing only the books of type Roman in each store like this

<mat-select formControlName="booksForm">
                <mat-optgroup *ngFor="let store of stores" [label]="books.store" 
                 [disabled]="store.disabled">
                  <mat-option *ngFor="let romanBook of books" [value]="books.items.name">
                    {{ romanBook.name }}
                  </mat-option>
                </mat-optgroup>
 </mat-select>

I don't know how to loop over the data to get only the stores that contain books of type ROMAN and then loop over the roman books in that specific store to show them as mat-option, any ideas or help ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just iterate over your items and add them to a new array with objects corresponding to your stores

const books = {
  __typename: '',
  items: [
    {
      id:'',
      store: 1,
      type: 'ROMAN',
      name: 'blalala'
    },
    {
      id:'',
      store: 1,
      type: 'BIOGRAPHY',
      name: 'blalala'
    },
    {
      store: 1,
      id:'',
      type: 'ROMAN',
      name: 'blalala'
    },
    {
      id:'',
      store: 2,
      type: 'ROMAN',
      name: 'blalala'
    }
    ]
}

let stores = []

for (let i = 0; i < books.items.length; i++) {
  if (books.items[i].type === 'ROMAN') {
    const index = stores.findIndex(elem => elem.id === books.items[i].store)
    if (index != -1) {
      stores[index].books.push(books.items[i]) 
    }else{
      stores.push({id:books.items[i].store,books:[books.items[i]]})
    }
  }
}

console.log(stores)

You can remove if (books.items[i].type === 'ROMAN') if you want to have all your books whatever their type, and then in html template check weither it's ROMAN or anything else with a *ngIf

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda