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

122
Visualizações
How can I remove characters from a string given in an array

for self development purposes I want to create a function with two parameters - string and an array. It should return a string without the letters given in the array.

function filterLetters(str, lettersToRemove) {

}

const str = filterLetters('Achievement unlocked', ['a', 'e']);

Could someone give me any pointers on how to achieve this?

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

0

You can use regex with replaceAll to remove all char from the string.

If you want to consider upper case also then use 'gi' else no need for regex also. str.replaceAll(char, '')

const removeChar = (str, c) => str.replaceAll(new RegExp(`[${c}]`, "gi"), "");

const run = () => {
  const str = "Achievement unlocked";
  const chars = ["a", "e"];

  let result = str;
  chars.forEach((char) => {
    result = removeChar(result, char);
  });
  return result;
};

console.log(run());

about 4 years ago · Juan Pablo Isaza Relatório

0

For each letter to be replaced, remove it (i.e. replace it with ''). When done, return the updated string:

function filterLetters(str, lettersToRemove) {
    lettersToRemove.forEach(function(letter){
        str = str.replaceAll(letter, '');
    })
    return str
}

Also see How to replace all occurrences of a string in JavaScript.

about 4 years ago · Juan Pablo Isaza Relatório

0

One easy way to do this would be to just loop over each value in the array and call the replace string method on str with each indices character. The code below does this.

function filterLetters(str, lettersToRemove){
   for (const letter of lettersToRemove){
      str = str.replaceAll(letter,"");
   }
   return str;
}
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