我有关于在空格后获取字符串字符的问题,其中字符串是这样的数组值..
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 值,它没有方法
split
Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise.
- 对于您的任务,您需要另外使用 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/