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

82
Visualizações
Map array of locales and change each locale to language name

Using Next.js, I'm getting the current locale and all available locales for a language selection menu:

const {cl, al} = useContext(LangContext);
// cl = "en-US"
// al = ["en-US", "de-DE"]

I wrote this function to return the full language name:

const clAlias = ()=> {
    if (cl === "en-US") { return "English" };
    if (cl === "de-DE") { return "Deutsch" };
}

Now I want to map al , but instead of having ["en-US", "de-DE"] I want to have ["English", "Deutsch"]. It not only looks better but also makes it easier for the user to select their language. What would be the best way to do so?

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

0

How about an object.

const clAlias = {
  "en-US": "English",
  "de-DE": "Deutsch"
}
//...
al.map(x => clAlias[x])
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.from method. It return new array without change al

And don't forget add arguments in clAlias() like clAlias(cl)

const al = ["en-US", "de-DE"];
const clAlias = (cl) => {
  if (cl === "en-US") {
    return "English";
  }
  if (cl === "de-DE") {
    return "Deutsch";
  }
};

const answer = Array.from(al, (e) => clAlias(e));
console.log(answer);

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