2 回答

TA貢獻1744條經驗 獲得超4個贊
您可以將數組組合成一個數組并對其進行迭代,sendMail()為每個元素調用您的方法。
const users1 = [{
download: 'Release sale. 50% off!',
user: '[email protected]'
},
{
download: 'Release sale. 50% off!',
user: '[email protected]'
},
{
download: 'Release sale. 50% off!',
user: '[email protected]'
},
{
download: 'Release sale. 50% off!',
user: '[email protected]'
}
];
const users2 = [{
download: 'Test',
user: '[email protected]'
}];
const allUsers = [...users1, ...users2];
const groupedUsers = allUsers.reduce((acc, u) => {
const group = acc.find(x => x.download === u.download);
if (group) {
group.users.push(u.user);
return acc;
}
return [...acc, {
download: u.download,
users: [u.user]
}];
}, []);
console.log(groupedUsers)
使用groupedUsers上面的列表,您應該能夠根據download屬性向用戶組發送電子郵件。
groupedUsers.forEach(async group => {
await transporter.sendMail({
from: '"Test" <[email protected]>',
to: group.users,
subject: "Here is your file",
text: `Here is your download: ${group.download}`
});
});
我使用了 spread ( ...) 運算符來組合數組,但concat()如果您愿意,也可以使用。const allUsers = users1.concat(users2);
添加回答
舉報