gyro101
gyro101

Reputation: 15

How to remove vowels from a list of string in JavaScript

I am trying to write a function that will remove all vowels in a list of strings in JavaScript. I know how to do this with a single string however I am having problem when I apply an array of strings. am getting an error TypeError: strings.replace is not a function.

  var strings = ["bongo drums", "guitar", 
  "flute", "double bass", "xylophone","piano"];                          

   string = strings.replace( /[aeiou]/g, '' );              

  console.log(string); 

Upvotes: 1

Views: 12946

Answers (9)

MuCloud
MuCloud

Reputation: 1

function removeVowels(word){

let vowels = ['a','e','i','o','u'];
 let string = '';

   for(let i = 0; i < word.length; i++){
     if(!vowels.includes(word[i])){
        string += word[i]
       };
      };
return string 
};

 console.log(removeVowels('Hello'))

Upvotes: 0

Muhammed Ajmal M
Muhammed Ajmal M

Reputation: 1

function disemvowel(str) {             
    let newString = " ";                            
    for (let i = 0; i < str.length; i++)  
{           
      if (str[i] != "a" && str[i] != "e" && str[i] != "i" && str[i] != "o" && str[i] != "u")                                   
       {           
       newString += str[i];         
      };   
    };   
    return newString;   
  };  
  console.log(disemvowel('ajmal'));                      

Upvotes: -1

Robert Hubbard
Robert Hubbard

Reputation: 66

Using either a for loop or .map() to iterate through each string in your array and apply your method of replacing vowels.

Upvotes: 0

Nauman Bashir
Nauman Bashir

Reputation: 13

You are performing operation on an array of string so you have to use some loop or iterator to iterate over array and perform replace on individual item of that array. In code below I am using map function to loop and remove vowels from each index.

var strings = ["bongo drums", "guitar", 
 "flute", "double bass", "xylophone","piano"];      

var stringsWithOutVovels =  strings.map(function(item){
  return item.replace( /[aeiou]/g, '' ); 
});

console.log(stringsWithOutVovels);

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22534

You can use array#map() to iterate through your strings array.

var strings = ["bongo drums", "guitar", "flute", "double bass", "xylophone","piano"];           
var string = strings.map(x => x.replace(/[aeiou]/g, ''));
console.log(string);

Upvotes: 0

sumeet kumar
sumeet kumar

Reputation: 2648

var strings = ["bongo drums", "guitar", 
  "flute", "double bass", "xylophone","piano"];                          

   string = strings.map(x=>x.replace( /[aeiou]/g, '' ));              

  console.log(string); 

Upvotes: 1

user94559
user94559

Reputation: 60143

Here's a solution using map:

var strings = ["bongo drums", "guitar",
  "flute", "double bass", "xylophone", "piano"
];

strings = strings.map(string => string.replace(/[aeiou]/g, ''));

console.log(strings);

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386654

You need to iterate and replace each element with a given array of string.

var strings = ["bongo drums", "guitar", "flute", "double bass", "xylophone", "piano"];

strings = strings.map(function (string) {
    return string.replace(/[aeiou]/g, '');
});

console.log(strings);

Upvotes: 2

heinscott
heinscott

Reputation: 19

var strings = ["bongo drums", "guitar", "flute", "double bass", "xylophone","piano"];

string = strings.filter(function(item) { return item.replace( /[aeiou]/g, '' ));

Upvotes: 1

Related Questions