简体   繁体   English

如何为对象值分配两个不同的变量?

[英]How to assign two different variables to an object value?

Not sure If my title makes sense but here's what I have problem doing. 不知道我的标题是否有意义,但这是我在做题时遇到的问题。

I have two HTML outputs: 我有两个HTML输出:

let output1 = `<h1>Header</h1>`
let output2 = `<div> // content </div>`

And I have this: 我有这个:

let mailOptions2 = {
      from: '"xyz" <xyz@info.com>', // sender address
      to: mailList, // receiver or receivers
      subject: 'XYZ', // Subject line
      html:       // html body
  };

I want both output1 and output2 assigned to html . 我想将output1output2分配给html I tried to put both in an array but it kept giving undefined error. 我试图将两者都放入一个数组中,但它始终给出未定义的错误。 So I want output1 be sent to the first address in mailList, and so on. 所以我想将output1发送到mailList中的第一个地址,依此类推。 Solution? 解?

Try to concat them: 尝试吸引他们:

html: output1 + output2

EDIT: With the updated requirement; 编辑:随着更新的要求;

Put your variables in an array. 将变量放入数组中。

let output = [`<h1>Header</h1>`, `<div> content </div>`, ....]

Then create mailOptions variable inside a loop. 然后在循环内创建mailOptions变量。

for(i = 0; i < mailList.lngth; i++) {
    let mailOptions = {
        from: '"xyz" <xyz@info.com>',
        to: mailList[i],
        subjects: 'XYZ',
        html: output[i] 
    }
}

This should work: 这应该工作:

outputs = ["1st HTML", "2nd HTML"];

for(var i = 0; i < mailList.length; i++){
    let mailOptions2 = {
        from: '"xyz" <xyz@info.com>',
        to: mailList[i],
        subject: 'XYZ',
        html: outputs[i % outputs.length]
    };
    // send your email here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM