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

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

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