我有一个关于在字符串是这样的数组值的空格后获取字符串字符的问题..

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]
在相同问题上有经验的人可以帮助我吗?我被困住了。
感谢你们...

最佳答案

  • String.prototype.includes()返回 bool(boolean) 值,没有方法split
  • Array.prototype.filter()需要:


  • 对于您的任务,您需要另外使用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);

    10-05 20:42
    查看更多