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

97
Vistas
leave the original letter if can't find one to replace

I'm trying to transform a message with default font to the font "vaporwave", but when it finds a letter that isn't defined, it returns undefined, how could i make it skip that letter that wasn't defined and leave the original one?

font = {
    "A": "A",
    "B": "B",
    "C": "C",
    "D": "D",
    "E": "E",
    "F": "F",
    [...]
}

const string = "hello world"
const vapour = string.split('').map(letter => {
  return font[letter];
}).join('');

//return for example, "undefined EL L O [...]"
//i want: "h EL L O [...]"


about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can alternate the font[letter] with the original letter:

const vapour = string.split('').map(letter => {
  return font[letter] || letter;
}).join('');

Or, what I'd prefer would be to construct a regular expression from the keys of the object:

const pattern = new RegExp('[' + Object.keys(font).join('') + ']', 'g');
const vapour = string.replace(pattern, char => font[char]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Nullish coalescing operator (??) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

const vapour = string.split('').map(letter => {
  return font[letter] ?? letter;
}).join('');
about 4 years ago · Juan Pablo Isaza Denunciar

0

Why not just check if the letter is present in the dictionary ?

font = {
    "A": "A",
    "B": "B",
    "C": "C",
    "D": "D",
    "E": "E",
    "F": "F",
}

const string = "ABCDEFG123"
const vapour = string.split('').map(letter => {
  if (letter in font) {
    return font[letter];
  } else {
    return letter
  }
}).join('');
console.log(string, "-->", vapour);

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