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 PattonAsked:
Who is Participating?
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

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?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michael PattonAuthor 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

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

Michael PattonAuthor Commented:
Thanks for your assistance.  The non-profit is happy with the results...and the price (pro bono)!
Julian HansenCommented:
You are welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.