I have an array of strings:
我有一个字符串数组:
let arr = ["1", ":", "3", "0", " ", "P", "M"];
I want to remove the ":"
and the whitespace using one filter helper. Is this possible? It works if I chain the methods together.
我想使用一个过滤器助手删除“:”和空格。这可能吗?如果我将方法链接在一起,它就有效。
arr = arr.filter(n => n !== ":").filter(n => n !== " ");
Is there a way that I could run this inside of only one filter method without chaining it? I realize I could also get this done with map()
and run a conditional for string comparisons, but I was curious if it could be done with filter()
.
有没有一种方法可以在没有链接的情况下只在一个过滤方法中运行它?我意识到我也可以用map()完成这个并运行一个条件进行字符串比较,但我很好奇是否可以用filter()完成。
5 个解决方案
#1
4
Chain the conditionals with an AND operator, like this:
使用AND运算符链接条件,如下所示:
arr.filter(n => n !== ":" && n !== " ");
#2
2
You approach only needs to join the conditions using AND logical operator
您只需要使用AND逻辑运算符来加入条件
arr.filter(n => n !== ":" && n !== " ");
^^
This approach uses the function test
as a handler for the function filter
此方法使用函数test作为函数过滤器的处理程序
This is the regex: [:\s]
这是正则表达式:[:\ s]
let arr = ["1", ":", "3", "0", " ", "P", "M"],
result = arr.filter(s => !/[:\s]/.test(s));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
As academy approach you can join replace and split
Regexp: /[:\s]/g
Regexp:/ [:\ s] / g
Assuming the other elements don't have spaces nor colons
假设其他元素没有空格或冒号
let arr = ["1", ":", "3", "0", " ", "P", "M"],
result = arr.join('').replace(/[:\s]/g, '').split('');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
1
Use a regular expression
使用正则表达式
arr.filter(n => !n.match(/[:\s]/) );
it will match any of the values inside [
and ]
它将匹配[和]内的任何值
#4
1
I will rather do it by making blacklist array
我宁愿通过制作黑名单数组来做到这一点
let arr = ["1", ":", "3", "0", " ", "P", "M"],
notAllowed = [' ', ':'];
console.log(arr.filter(a => !notAllowed.includes(a)));
#5
0
arr = arr.filter(n => n !== ":" && n !== " ");
Pretty simple really! :)
真的很简单! :)
#1
4
Chain the conditionals with an AND operator, like this:
使用AND运算符链接条件,如下所示:
arr.filter(n => n !== ":" && n !== " ");
#2
2
You approach only needs to join the conditions using AND logical operator
您只需要使用AND逻辑运算符来加入条件
arr.filter(n => n !== ":" && n !== " ");
^^
This approach uses the function test
as a handler for the function filter
此方法使用函数test作为函数过滤器的处理程序
This is the regex: [:\s]
这是正则表达式:[:\ s]
let arr = ["1", ":", "3", "0", " ", "P", "M"],
result = arr.filter(s => !/[:\s]/.test(s));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
As academy approach you can join replace and split
Regexp: /[:\s]/g
Regexp:/ [:\ s] / g
Assuming the other elements don't have spaces nor colons
假设其他元素没有空格或冒号
let arr = ["1", ":", "3", "0", " ", "P", "M"],
result = arr.join('').replace(/[:\s]/g, '').split('');
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#3
1
Use a regular expression
使用正则表达式
arr.filter(n => !n.match(/[:\s]/) );
it will match any of the values inside [
and ]
它将匹配[和]内的任何值
#4
1
I will rather do it by making blacklist array
我宁愿通过制作黑名单数组来做到这一点
let arr = ["1", ":", "3", "0", " ", "P", "M"],
notAllowed = [' ', ':'];
console.log(arr.filter(a => !notAllowed.includes(a)));
#5
0
arr = arr.filter(n => n !== ":" && n !== " ");
Pretty simple really! :)
真的很简单! :)