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

168
Visualizações
Use an array as a reference for initials/shortcuts

I've got datas from an external source that uses initials to refer to specific type of data

Initial Value
A Attelé
AB Abajour
B Base
... ...

I want to be able to get back the value. As I know the value for each shortcuts, I could do something like that :

function getValue(initial)
{
    if(initial == "A") return "Attelé";
    else if(initial == "AB") return "Abajour";
    else if(initial == "B") return "Base";
}

But this is not a good practice as I will have to add a new comparaison for every initial

So i'm thinking about using two array as references :

var values = ["Attelé","Abajour","Base"];
var initialList = ["A", "AB","B"];

function getValue(initial)
{
    var index = initialList.indexOf(initial);
    if(index > -1) return values[index];
    else return null;
}

console.log(getValue("A"));

But here again, i think there is a better practice. To add a new initial, I still have to modify two variables, and if my arrays's length are not matching I could have an issue with the result. I would like to be able to change only one variable.

Hence my question : How can I use one object/array/table to match my initials, and what is the best practice to look at ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Can you not just put the data in an object

const names = {
  "a": "Attelé",
  "b": "Base"
}

and then access the data by using

names["a"]
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