Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!