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

140
Vistas
Return multiple "roles" from JSON Array

I am trying to return multiple "roles" to a DataTable. Every single role has a specific "title" and "class", which then is returned to the datatable column.

var $role_number = full['roles'];
var $roles = {
    1: { title: 'WorkShop Leader', class: 'badge-light-primary' },
    2: { title: 'Planner', class: 'badge-light-secondary' },
    3: { title: 'Operator', class: 'badge-light-warning' },
};

These are the roles available. And then I return this:

return (
    '<span class="badge rounded-pill ' + $roles[$role_number].class + '">' + 
    $roles[$role_number].title + '</span>'
);

This works if the JSON Object has only one role (for example, role 1: WorkShop Leader). But I want "roles" to be an array, because one user can have more than one role.

So this user has only one role:

{
    "id": 1,
    "first_name": "Albert",
    "second_name": "Cognoms",
    "roles": [1],
    "status": 1
}

And it works properly: enter image description here

But this user has two roles, and it doesn't show both badges:

{
    "id": 1,
    "first_name": "Albert",
    "second_name": "Cognoms",
    "roles": [1, 2],
    "status": 1
}

enter image description here

If someone knows a better way to display role badges using an array, I would really appreciate the help. I know the solution must be something easy, but I can't get my mind clear on this.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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