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

148
Views
How could I loop through an array and return the values ​dynamically?

I have a problem, I have a function that returns values ​​from an array, but I wanted to return these values ​​dynamically.

const AvailableUserRoles = [
  {
    label: 'Administrator',
    value: 1
  },  
  {
    label: 'Count',
    value: 2
  },
  {
    label: 'Test',
    value: 5
  }
]

This is my function, which receives a parameter, which is a numeric value.

function getValue(item){
    if(item == AvailableUserRoles[0].value){
      return 'Administrator'

    }else if(item == AvailableUserRoles[1].value){
      return 'Count'

    }else if(item == AvailableUserRoles[3].value){
      return 'Test'

    }
  }      
}

I would like to do this check with dynamic values ​​as it would be easier to add new options later. No need to continue using AvailableUserRoles[].value

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use Array.find to find the matching value dynamically.

const AvailableUserRoles = [
    {
        label: 'Administrator',
        value: 1
    },
    {
        label: 'Count',
        value: 2
    },
    {
        label: 'Test',
        value: 5
    }
]
function getValue(item) {
    const matchItem = AvailableUserRoles.find(node => node.value === item);
    return matchItem ? matchItem.label : "";
}
console.log(getValue(1)); // Returns 'Administrator'
console.log(getValue(2)); // Returns 'Count'
console.log(getValue(5)); // Returns 'Test'
console.log(getValue(6)); // Returns ''

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