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

70
Views
react adding new array method to return array's object item by it's key

I have some enum objects and i want to get its lebel by it's value.

Let's say this is my enum list

const enumList = {
    businessType: [
        { value: "b", label: "B2B" },
        { value: "s", label: "SAAS" },
        { value: "c", label: "C2C" },
    ],
    userType: [
        { value: "A", label: "Admin" },
        { value: "s", label: "Super User" },
        { value: "t", label: "trainer" },
    ]
}

So now I want to get a particular object label by its key.

like this,

enumList.userType.getLabel('s')'
// super user

I mean, i want to make it re-usable, currently i am doing like this and i dont like it

index = enumList.userType.findIndex(x => x.value ==="s");
enumList.userType[index].label
// super user

Does anyone know how can i make it re-usable to access it from globally like this enumList.userType.getLabel('s) ?

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

0

See Why is extending native objects a bad practice?

It looks like you want a one-liner, so just use find

const { label } = enumList.userType.find(userType => userType.value === 's');
about 4 years ago · Juan Pablo Isaza Report

0

This custom Array method will allow you to do this enumList.userType.getLabel('s') and receive 'Super User'

Array.prototype.getLabel = function(val) {
  return this.find(({value}) => value === val).label
}
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!