如何从JavaScript中的字符串列表中删除元音

时间:2017-10-10 17:17:46

标签: javascript

我正在尝试编写一个函数,它将删除JavaScript中字符串列表中的所有元音。我知道如何使用单个字符串执行此操作,但是当我应用字符串数组时遇到问题。我收到错误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); 

8 个答案:

答案 0 :(得分:3)

以下是使用map

的解决方案



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

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

console.log(strings);




答案 1 :(得分:2)

您需要使用给定的字符串数组迭代并替换每个元素。

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

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

console.log(strings);

答案 2 :(得分:1)

var strings = [" bongo鼓","吉他",   "长笛","低音提琴","木琴","钢琴"];

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

答案 3 :(得分:1)



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

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

  console.log(string); 




答案 4 :(得分:0)

使用for循环或.map()迭代数组中的每个字符串并应用替换元音的方法。

答案 5 :(得分:0)

您可以使用array#map()来遍历strings数组。



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




答案 6 :(得分:0)

您正在对字符串数组执行操作,因此您必须使用一些循环或迭代器来迭代数组并对该数组的单个项执行替换。在下面的代码中,我使用map函数来循环并从每个索引中删除元音。

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

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

console.log(stringsWithOutVovels);

答案 7 :(得分:-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'));