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

208
Visualizações
How to remove elements from an array with splice where there's consecutive matches?

I have a function that removes vowels from a string, and then returns the string. My problem is that where there's two or more consecutive vowels it will only remove one of them. I can run the 'for' loop twice to fix the issues, but that doesn't seem right. Is there anything I could change?

let string = "I'd always thought lightning was something only I could see.";
let vowels = ['a', 'e', 'i', 'o', 'u']

function removeVowels(str) {
  let stringArray = str.split('');
  for (let letter of stringArray){
    if (vowels.includes(letter.toLowerCase())){
      let index = stringArray.indexOf(letter); 
      stringArray.splice(index, 1);
    }
  }
  return stringArray.join('');
}

Returns:

"'d lwys thught lghtnng ws smthng nly  culd se."
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When iterating forward, the splice shifts the next element of the array backward, under the iterating index; then incrementing the index misses that element.

It's fixable in a few ways, but doing the least damage to your approach, just iterate the letters in reverse so the iteration and deletion don't interfere with one another...

let string = "I'd always thought lightning was something only I could see.";
let vowels = ['a', 'e', 'i', 'o', 'u'];

function removeVowels(str) {
  let stringArray = str.split('');
  for (let i=stringArray.length-1; i>=0; i--){
    let letter = stringArray[i];
    if (vowels.includes(letter.toLowerCase())){
      stringArray.splice(i, 1);
    }
  }
  return stringArray.join('');
}

console.log(removeVowels(string))

Since we're iterating over the indexes, a side benefit is that we don't have to search the array with indexOf().

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