Reputation: 11
I am building a condition script that searches a comma-separated list of email recipients. There can be multiple recipients and the condition script searches for three (3) specific addresses.
This is not a full-fledged script - it can only include a conditional statement. Our issue is that this field has a character limit of 255... and we have reached it.
Here is the current condition:
email.recipients.toLowerCase().indexOf('email_1') >= 0 || email.recipients.toLowerCase().indexOf('email_2') >= 0 || email.recipients.toLowerCase().indexOf('email_3') >= 0
This is within our character limit - and works - but now we need to add another email address to search for. Is there any way of shortening this condition?
Upvotes: 1
Views: 122
Reputation: 1988
If you can use ES6 (you don't need to worry about Internet Explorer), you can use an arrow function:
['email_1','email_2','email_3'].some(e=>email.recipients.toLowerCase().indexOf(e)+1);
Upvotes: 1
Reputation: 15154
use some
:-
['email_1', 'email_2', 'email_3'].some(function(e) {
return email.recipients.toLowerCase().indexOf(e) >= 0;
});
Upvotes: 0
Reputation: 6151
Not the perfect answer of course, but you can shorten a little bit with using an alias (be careful about var conflicts, because you cannot use the var
keyword inside this expression, meaning the var will be global, it also might not work in strict mode i think).
Just made an answer because it was too long for a comment.. hope ite helps.
var email = {}; email.recipients = 'email_1,email_2,email_3';
console.log((a = email.recipients.toLowerCase()) && (a.indexOf('email_1') >= 0 || a.indexOf('email_2') >= 0 || a.indexOf('email_3') >= 0));
Upvotes: 0
Reputation: 664538
Depending on the particular email addresses, a regex solution might be simpler:
/email_[1-3]/i.test(email.recipients)
Upvotes: 0