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

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?
zephyr_hex (Megan)DeveloperCommented:
Can you provide an example of the arrays so we can better understand your question?
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 = ["", "", "", "", ""];
var recipient = emails[cmtes.indexOf(cmte)];

Open in new window

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: "" };
var object2 = { cmtes: "Per", email: "" };
console.log(array1.find(x => x.cmtes == "Per")); //{cmtes: "Per", email: ""}
console.log(array1.find(x => x.cmtes == "Per").email); //

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

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.
<select id="ddl">
  <option value="">Select Cmtes</option>
  <option value="MI" data-email="">MI</option>
  <option value="Per" data-email="">Per</option>

Open in new window

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

Open in new window

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
var cmte = e.values[1];
var cmtes = {
    'MI' : '',
    'Per': '',
    'FC' : '',
    'P&F': '',
    'ME' : ''
var recipient = cmtes[cmte] || false;
if (recipient) {
  // rest of code here
else {
  // handle choice not found here

Open in new window


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)!
Julian HansenCommented:
You are welcome.
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.