【问题标题】:How to remove vowels from a list of string in JavaScript如何从 JavaScript 中的字符串列表中删除元音
【发布时间】:2018-03-22 04:40:27
【问题描述】:

我正在尝试编写一个函数,该函数将删除 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); 

【问题讨论】:

  • 在for里面做?
  • strings 是一个数组而不是一个单独的字符串,您需要使用 map 或遍历每个字符串来做到这一点
  • 查看这个链接,它似乎突出了你的问题:stackoverflow.com/questions/26742310/…

标签: javascript


【解决方案1】:

var strings = ["邦戈鼓", "吉他", “长笛”、“低音提琴”、“木琴”、“钢琴”];

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

【讨论】:

  • 使用Array#filter() 的副作用是从数组中删除所有只有元音的字符串。这是否需要是另一个问题。
【解决方案2】:

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

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

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

console.log(strings);

【讨论】:

    【解决方案3】:

    这是使用map的解决方案:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          您可以使用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);

          【讨论】:

            【解决方案7】:

            您正在对字符串数组执行操作,因此您必须使用一些循环或迭代器来迭代数组并对该数组的单个项目执行替换。在下面的代码中,我使用 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);
            

            【讨论】:

              【解决方案8】:

              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'));                      

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-11-23
                • 2021-05-23
                • 1970-01-01
                • 2014-06-04
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多