javascript - 在javascript上获取数组值空间后的字符

标签 javascript vue.js

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

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]

谁有同样问题的经验可以帮助我吗?我卡住了。 谢谢你们...

最佳答案

  1. String.prototype.includes()返回 bool 值,它没有方法 split

  2. Array.prototype.filter()需要:

Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise.

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

关于javascript - 在javascript上获取数组值空间后的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63015191/

相关文章:

javascript - 如何在 Web 容器中自定义滚动条的位置

javascript - 导航菜单 JQuery 问题

javascript - 这是确定 JavaScript 变量类型的有效方法吗?

javascript - Angular 模板在 http 请求后未更新

javascript - VueJs 路由器链接在实时服务器上不起作用

javascript - Buefy datepicker如何输出 'yyyy-MM-dd'字符串?

typescript - 导出默认类扩展 Vue 无法从模板访问

javascript - VSCode 问题 : Vue example doesn't work in Firefox/Chrome, 但它可以在 jsfiddle 中使用

javascript - 从 PHP 触发器切换 Bootstrap 模式

vue.js - vuetify 选择或组合框绑定(bind)项目