Search code examples
javascriptarrayssplice

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

Solution

  • 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().