我试图循环一个数组并过滤掉所有与特定值不匹配的项目。
例如,我有这个数组:
const emails = ["nick@hotmail.com", "nick@yahoo.com", "nick@gmail.com", "bob@yahhoo.com", "bob@gmail.com", "boc@test.com"];我想过滤掉以“结尾”的电子邮件
*@hotmail.com *@gmail.com
我已经给它一个去了,但这不起作用:
const filtered = emails.filter((email) => { return !email.includes('@hotmail.com') || !email.includes('@gmail.com'); });上例中的首选输出是:
["nick@yahoo.com", "bob@yahhoo.com", "boc@test.com"]I am trying to loop through an array and filter out all the items that do not match specific values.
For example I have this array:
const emails = ["nick@hotmail.com", "nick@yahoo.com", "nick@gmail.com", "bob@yahhoo.com", "bob@gmail.com", "boc@test.com"];I would like to filter out emails that end in"
*@hotmail.com *@gmail.com
I have given it a go and got this but this doesn't work:
const filtered = emails.filter((email) => { return !email.includes('@hotmail.com') || !email.includes('@gmail.com'); });The preferred output from the example above would be:
["nick@yahoo.com", "bob@yahhoo.com", "boc@test.com"]最满意答案
替换或|| 用和&&
如果您选择所有不包含Hotmail或不包含Gmail的电子邮件,您将获得所有不包含两者的电子邮件,这不是您的目标。 你想得到所有那些不包含hotmail和gmail的东西!
const filtered = emails.filter((email) => { return !email.includes('@hotmail.com') && !email.includes('@gmail.com'); });Replace the or || with an and &&
If you select all emails which do not contain hotmail OR do not contain gmail, you'll get all of them which don't contain both which isn't your objective. You want to get all of those that do not contain both hotmail and gmail instead!
const filtered = emails.filter((email) => { return !email.includes('@hotmail.com') && !email.includes('@gmail.com'); });更多推荐
发布评论