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

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

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

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

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 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