Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!