Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

84
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda