gpt4 book ai didi

javascript - ES6 为多个数组运行不同的函数

转载 作者:行者123 更新时间:2023-11-29 16:30:59 26 4
gpt4 key购买 nike

我有一个 sendEmail 函数,它需要根据用户在数组中的位置发送不同的电子邮件。例如,我有以下数据:

[ 
{ download: 'Release sale. 50% off!',
user: 'test1@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test2@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test3@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test4@gmail.com' }
]
[
{ download: 'Test',
user: 'test5@gmail.com'
}
]

对于每个数组,我需要累积所有 user 电子邮件和 download 字符串并运行以下函数:

await transporter.sendMail({
from: '"Test" <noreply@test.com>',
to: [email array here],
subject: "Here is your file",
text: `Here is your download: ${download}`
})

最佳答案

这可以简单地通过减少来实现

const emailReduced = [ 
{ download: 'Release sale. 50% off!',
user: 'test1@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test2@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test3@gmail.com' },
{ download: 'Release sale. 50% off!',
user: 'test4@gmail.com' }
].reduce((acc,{download, user}) => {
return {
...acc,
to: [...acc.to, user],
text: `Here is your download: ${download}`
}
}, {from: 'test <test@noreply.com>', subject: 'here is your file', to:[]})

await transporter.sendMail(emailReduced)

关于javascript - ES6 为多个数组运行不同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com