Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

73
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda