【问题标题】:get character after space of array value on javascript在javascript上的数组值空间后获取字符
【发布时间】:2020-11-10 20:58:02
【问题描述】:

我有关于在空格之后获取字符串字符的问题,其中字符串是这样的数组值..

arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];

output = arr.filter(function (p) {
            if (!Number(p)) {  // get string value 
               return p.includes('aaa').split(' ').pop();
            }
         });

console.log(output)

我收到错误“TypeError:p.includes(...).split 不是函数”

如果我删除 .split(' ').pop();

array['aaa: 2','aaa: 5','aaa: 6']

我只想要这样的输出

array [2,5,6]

任何有相同问题经验的人可以帮助我吗?我被困住了。 谢谢各位...

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:
    1. filter 方法仅有条件地返回一些过滤后的值 数组。
    2. 包括方法返回布尔类型值。所以你不能使用带有布尔类型值的 split 方法。

    如果您想从主数组中获取自定义数组,则可以使用 reduce 方法。

    const arr = ['aaa: 2', 'aaa: 5', 'aaa: 6', 3, 7, 8];
    
    const output = arr.reduce((value, key) => {
      if (!Number(key) && key.includes('aaa')) {
        value.push(parseInt(key.split(' ').pop()));
      }
      return value;
    }, []);
    
    console.log(output);

    【讨论】:

      【解决方案2】:

      首先,.filter 是在这里使用的错误方法,它要么期望 true 要么 false 来保留/删除数组中的给定项目。请改用 .map

      其次,.includes 返回 true 或 false,因此尝试拆分布尔值将不起作用

      如果您想删除上次编辑中提到的数字,请先过滤。试试这个:

      // first filter out numbers
      const output = arr.filter(function(p) {
          return !Number(p)
      }
      
      // then get the numbers out
      output = output.map(function(p) {
          if (p.includes('aaa')) { 
              return Number(p.split(' ').pop());
          }
      });
      

      【讨论】:

      • 输出:数组['aaa: 2','aaa: 5','aaa: 6']
      【解决方案3】:
      1. String.prototype.includes()返回bool值,没有方法split

      2. Array.prototype.filter() 接受:

      函数是一个谓词,用来测试数组的每个元素。返回 保留元素为 true,否则为 false。

      1. 对于您的任务,您需要另外使用Array.prototype.map()

      const arr = ['aaa: 2', 'aaa: 5', 'aaa: 6', 3, 7, 8];
      
      const output = arr
          .filter((p) => {
              return Number(p) ? false : p.includes('aaa');
          })
          .map((p) => Number(p.split(' ').pop()));
      
      console.log(output);

      【讨论】:

      • 完美兄弟.. Array(3) [ 2, 5, 6 ]
      【解决方案4】:

      如果值是字符串,可以使用下面的正则表达式提取空格后面的数字,例如

      /^\w+:\s(\d+)$/
      

      匹配组 #1 将是数字,您只需将其解析为整数。

      const transform = (arr) =>
        arr.map(val => typeof val === 'string'
          ? parseInt(val.match(/^\w+:\s(\d+)$/)[1], 10)
          : val)
      
      console.log(transform(['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8]))

      【讨论】:

        【解决方案5】:

        Ciao,试试这个:

        var arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
        
        console.log(arr.map(el => {
           if(typeof el === "string") {
              return parseInt(el.split(": ")[1]);
           }
        }).filter(el => el!== undefined))

        【讨论】:

          【解决方案6】:

          看起来您需要数组中的数字?使用Array.map 和一点RegEx 魔法:

          console.log(
            JSON.stringify( ['aaa: 2', 'aaa: 5', 'aaa: 6', 3 , 7 , 8]
              .map( v => /:\s+\d$/.test(v) ? Number(v.split(": ")[1]) : v) ) );
           
          // only numbers from strings?
          console.log(
            JSON.stringify( ['aaa: 2', 'aaa: 5', 'aaa: 6', 3 , 7 , 8]
             .filter( v => isNaN(+v) )
             .map( v => Number( v.split(": ")[1] ) ) ) );

          【讨论】:

            【解决方案7】:

            Array.prototype.filter() 函数旨在仅过滤数组,无需修改。要结合修改和文件串,请改用 reduce:

            const arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
            
            const output = arr.reduce((result, current) => {
                        if (!Number(current)) {  // get string value  
                           if(current.includes('aaa')) {
                           return [...result, current.split(' ').pop()];
                           }
                        }
                        return result
                     }, []);
            
            console.log(output)

            【讨论】:

              【解决方案8】:

              创建一个对数字进行分组的正则表达式,然后将其提取出来。

              const regex = /aaa:\s(\d+)/
              const matches = regex.exec(p)
              return matches && matches.length > 0 && matches[0]
              

              【讨论】:

                【解决方案9】:

                您可以使用.reduce

                const arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
                
                const output = arr.reduce((acc, arrayValue) => {
                
                    const [key, value] = arrayValue.toString().split(': ');
                
                    if (key.includes('aaa')) {
                      acc.push(parseInt(value))
                    }
                    
                    return acc;
                    }, []);
                
                console.log(output)

                【讨论】:

                  猜你喜欢
                  • 2023-04-03
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-09-22
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-03-29
                  • 1970-01-01
                  相关资源
                  最近更新 更多