Jia-Luo
Jia-Luo

Reputation: 3303

Data loop using nodejs mailer template

I'm using node.js mailer module to send email template. I manage to send the template with the correct json replacements to the designated email address.

However, I am wondering if there's a shortcut to set the values of the replacement when there's too many similar replacements.

For example, i have an object "userNameList". It contains a list of usernames. I want to send the list of usernames using an email template. Instead of...

data: {
  "username1":userNameList[1],
  "username2":userNameList[2],
  "username3":userNameList[3],
  "username4":userNameList[4],
  "username5":userNameList[5],
  "username6":userNameList[6],
              ...
  "username100":userNameList[100]
}

Is there a way to reduce the number of lines and for the code to work more effectively?

thanks.

Upvotes: 0

Views: 428

Answers (1)

Tuomas Artman
Tuomas Artman

Reputation: 76

You can use the following:

data = {};
for(var i=0; i<userNameList.length; i++) {
   data["username" + (i+1)] = userNameList[i]; // Array indexes start at 0
}

Upvotes: 1

Related Questions