Javascript 如何循环遍历字母数组并返回数组中的第一个连续辅音?

Javascript 如何循环遍历字母数组并返回数组中的第一个连续辅音?,javascript,arrays,for-loop,Javascript,Arrays,For Loop,我试图从数组中只删除第一个连续的辅音,这样它就会返回[“e”,“l”,“l”,“o”] 我可能把你的问题解释错了,但我认为它的意思是“返回一个数组,去掉所有前导辅音” 无论如何,这似乎是正则表达式的一个很好的用例: var arr=["h","e","l","l","o"] // hat-tip to @minitech in the comments for improving this line... var result = arr.join('').replace(/^[^aeiou]

我试图从数组中只删除第一个连续的辅音,这样它就会返回[“e”,“l”,“l”,“o”]


我可能把你的问题解释错了,但我认为它的意思是“返回一个数组,去掉所有前导辅音”

无论如何,这似乎是正则表达式的一个很好的用例:

var arr=["h","e","l","l","o"]

// hat-tip to @minitech in the comments for improving this line...
var result = arr.join('').replace(/^[^aeiou]+/, '').split('');

console.log(result);
// [ 'e', 'l', 'l', 'o' ]
说明:

  • arr.join(“”)
    将数组转换为字符串
  • .replace(/^[^aeiou]+/,'')
    删除所有前导非元音字符
  • .split(“”)
    将结果转换为字符数组

    • 我可能把你的问题解释错了,但我认为它的意思是“返回一个数组,去掉所有前导辅音”

      无论如何,这似乎是正则表达式的一个很好的用例:

      var arr=["h","e","l","l","o"]
      
      // hat-tip to @minitech in the comments for improving this line...
      var result = arr.join('').replace(/^[^aeiou]+/, '').split('');
      
      console.log(result);
      // [ 'e', 'l', 'l', 'o' ]
      
      说明:

      • arr.join(“”)
        将数组转换为字符串
      • .replace(/^[^aeiou]+/,'')
        删除所有前导非元音字符
      • .split(“”)
        将结果转换为字符数组

        • 我可能把你的问题解释错了,但我认为它的意思是“返回一个数组,去掉所有前导辅音”

          无论如何,这似乎是正则表达式的一个很好的用例:

          var arr=["h","e","l","l","o"]
          
          // hat-tip to @minitech in the comments for improving this line...
          var result = arr.join('').replace(/^[^aeiou]+/, '').split('');
          
          console.log(result);
          // [ 'e', 'l', 'l', 'o' ]
          
          说明:

          • arr.join(“”)
            将数组转换为字符串
          • .replace(/^[^aeiou]+/,'')
            删除所有前导非元音字符
          • .split(“”)
            将结果转换为字符数组

            • 我可能把你的问题解释错了,但我认为它的意思是“返回一个数组,去掉所有前导辅音”

              无论如何,这似乎是正则表达式的一个很好的用例:

              var arr=["h","e","l","l","o"]
              
              // hat-tip to @minitech in the comments for improving this line...
              var result = arr.join('').replace(/^[^aeiou]+/, '').split('');
              
              console.log(result);
              // [ 'e', 'l', 'l', 'o' ]
              
              说明:

              • arr.join(“”)
                将数组转换为字符串
              • .replace(/^[^aeiou]+/,'')
                删除所有前导非元音字符
              • .split(“”)
                将结果转换为字符数组

              您的实际权限关闭正确的功能如下所示

              arr=["h","e","l","l","o"];
              vowel=["a","e","i","o","u"];
              consonants=[];
              
              for (i = 0; i < arr.length; i++){
                  if(vowel.indexOf(arr[i]) == -1 ) {
                     consonants.push(arr[i]);
                     arr= arr.slice(consonants.length)
              }else { return arr }   
              
              }
              
              arr=[“h”、“e”、“l”、“l”、“o”];
              元音=[“a”,“e”,“i”,“o”,“u”];
              辅音=[];
              对于(i=0;i

              for循环有点不正确,在函数中,恒频变量被命名为两个不同的东西…您有恒频恒频

              arr=["h","e","l","l","o"];
              vowel=["a","e","i","o","u"];
              consonants=[];
              
              for (i = 0; i < arr.length; i++){
                  if(vowel.indexOf(arr[i]) == -1 ) {
                     consonants.push(arr[i]);
                     arr= arr.slice(consonants.length)
              }else { return arr }   
              
              }
              
              arr=[“h”、“e”、“l”、“l”、“o”];
              元音=[“a”,“e”,“i”,“o”,“u”];
              辅音=[];
              对于(i=0;i

              for循环有点不正确,在函数中,恒频变量被命名为两个不同的东西…您有恒频恒频

              arr=["h","e","l","l","o"];
              vowel=["a","e","i","o","u"];
              consonants=[];
              
              for (i = 0; i < arr.length; i++){
                  if(vowel.indexOf(arr[i]) == -1 ) {
                     consonants.push(arr[i]);
                     arr= arr.slice(consonants.length)
              }else { return arr }   
              
              }
              
              arr=[“h”、“e”、“l”、“l”、“o”];
              元音=[“a”,“e”,“i”,“o”,“u”];
              辅音=[];
              对于(i=0;i

              for循环有点不正确,在函数中,恒频变量被命名为两个不同的东西…您有恒频恒频

              arr=["h","e","l","l","o"];
              vowel=["a","e","i","o","u"];
              consonants=[];
              
              for (i = 0; i < arr.length; i++){
                  if(vowel.indexOf(arr[i]) == -1 ) {
                     consonants.push(arr[i]);
                     arr= arr.slice(consonants.length)
              }else { return arr }   
              
              }
              
              arr=[“h”、“e”、“l”、“l”、“o”];
              元音=[“a”,“e”,“i”,“o”,“u”];
              辅音=[];
              对于(i=0;i
              for循环有点不正确,函数中的恒常变量被命名为两个不同的东西…这里的循环中有恒常变量和恒常变量

              for ( i in arr){
              
              您应该为
              循环使用正常的
              <代码>for in
              用于迭代属性并在中迭代

              在这里的情况下,

              if(vowel.indexOf(arr[i]) =-1 )
              
              您需要使用
              ==
              检查是否相等<代码>=
              仅用于分配。(
              ==
              也是一个很好的选择,用于严格相等。)

              在这里的声明中,

              consonant+=arr[i];
              
              您第一次将
              辅音
              从数组转换为字符串(假设
              辅音
              只是一个输入错误)。如果只使用其
              length
              属性,则两者都可以,但应保持一致,要么使用空字符串
              '
              初始化它,要么使用
              .push()
              方法附加到它

              在重新分配
              arr
              的过程中

              arr= arr.slice(consonant.length)
              
              您将丢失正在枚举的原始对象,这将使索引在将来的迭代中发生倾斜。您还将每次切掉
              辅音.length
              ,这将导致总共删除1、3、6、10等字符。也许最好打破循环,在最后做一次切片

              如果尚未声明变量,则还应声明变量。总而言之

              var arr = ["h", "e", "l", "l", "o"];
              var vowels = ["a", "e", "i", "o", "u"];
              var consonants = [];
              
              for (var i = 0; i < arr.length; i++) {
                  var c = arr[i];
              
                  if (vowels.indexOf(c) == -1) {
                      consonants.push(c);
                  } else {
                      // using `i` in place of `consonants.length` here would work just as well
                      return arr.slice(consonants.length);
                  }
              }
              
              var arr=[“h”、“e”、“l”、“l”、“o”];
              可变元音=[“a”、“e”、“i”、“o”、“u”];
              var辅音=[];
              对于(变量i=0;i
              在这里的循环中

              for ( i in arr){
              
              您应该为
              循环使用正常的
              <代码>for in
              用于迭代