Javascript DoWhile loop; I think

I am writing some Javascript for a non-profit to send the contents of a Google form to specific email addresses dependent upon the value in one of the form fields.  I have declared variables which pull the form field values (tested successfully). I have also created two array variables: one for the list of possible choices in the form field and another with the corresponding emails.  I assume I need to create a function which loops through the choices array to find the position of the matching value and then assigns the corresponding value in the email array to the sender variable; but I am not quite sure of the mechanics of that.  Help would be appreciated.
Michael PattonHigher Ed IS Instructor/ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
Matching based on position is prone to bugs.

Is there a reason why you aren't using an object that maps the choice to the email ?  Or use a data attribute on the HTML element that has the choices, and store the value of the email in the data attribute?
0
zephyr_hex (Megan)DeveloperCommented:
Can you provide an example of the arrays so we can better understand your question?
0
Michael PattonHigher Ed IS Instructor/ConsultantAuthor Commented:
Here is an example of what I was trying to do (obviously not well):

var cmte = e.values[1];
var cmtes = ["MI", "Per", "FC", "P&F", "ME"];
var emails = ["mail1@gmail.com", "mail2@gmail.com", "mail3@gmail.com", "mail4@gmail.com", "mail5@gmail.com"];
var recipient = emails[cmtes.indexOf(cmte)];

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

zephyr_hex (Megan)DeveloperCommented:
Yeah , that's a bad approach because you're relying on the index in one array to find a value in another array.

You'd be better off with an object.
Here are a couple of examples:
//build an array of objects
var array1 = [];
var object1 = { cmtes: "MI", email: "mail1@gmail.com" };
array1.push(object1);
var object2 = { cmtes: "Per", email: "mail2@gmail.com" };
array1.push(object2);
console.log(array1);
console.log(array1.find(x => x.cmtes == "Per")); //{cmtes: "Per", email: "mail2@gmail.com"}
console.log(array1.find(x => x.cmtes == "Per").email); //mail2@gmail.com

//OR
//build an object with cmtes values as properties
var object3 = { 'MI' : 'mail1@gmail.com', 'Per': 'mail2@gmail.com'};
console.log(object3['Per']); //mail2@gmail.com

Open in new window


Or, depending on how your HTML is structured / built, you could create a data attribute on your HTML element that has the "cmtes" values.  Suppose that element is a drop down list.
HTML
<select id="ddl">
  <option value="">Select Cmtes</option>
  <option value="MI" data-email="mail1@gmail.com">MI</option>
  <option value="Per" data-email="mail2@gmail.com">Per</option>
</select>

Open in new window


JS
var e = document.getElementById("ddl");
e.onchange = function() {
  var selected = e.options[e.selectedIndex].value; //selected value
  var email = e.options[e.selectedIndex].dataset.email; //email value
  console.log(email);
}

Open in new window

0
Julian HansenCommented:
Expanding on what Megan said. Make your choices keys and your emails values then you can simply access the corresponding email by the choice given.

Something like this
<script>
var cmte = e.values[1];
var cmtes = {
    'MI' : 'mail1@gmail.com',
    'Per': 'mail2@gmail.com',
    'FC' : 'mail3@gmail.com',
    'P&F': 'mail4@gmail.com',
    'ME' : 'mail5@gmail.com'
  };
  
var recipient = cmtes[cmte] || false;
if (recipient) {
  // rest of code here
}
else {
  // handle choice not found here
}
</script>

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael PattonHigher Ed IS Instructor/ConsultantAuthor Commented:
Thanks for your assistance.  The non-profit is happy with the results...and the price (pro bono)!
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.