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
<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
 
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
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 = ["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
 
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
 
Michael PattonAuthor 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
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.