假设我必须向列表中明天有约会的人发送电子邮件, 然而,当我编写电子邮件主题时,我想知道谁明天有多个约会,以便我可以配置主题。 我也不想一次发送多封电子邮件。我想发送一封电子邮件,说明您明天有一个约会或多个约会。 (如果知道这些约会的 ID 也很好。)
所以我有以下内容:
AllAppointments: [
{
id: 23,
name: "John",
email: "John@domain.com",
appointment_date: tomorrow,
appointment_category: 3,
time: "morning"
}, {
id: 17,
name: "Alex",
email: "Alex @domain.com",
appointment_date: tomorrow,
appointment_category: 3,
time: "morning"
},
{
id: 22,
name: "Bob",
email: "Bob@domain.com",
appointment_date: tomorrow,
appointment_category: 5,
time: "morning"
}, {
id: 35,
name: "John",
email: "John@domain.com",
appointment_date: tomorrow,
appointment_category: 4,
time: "afternoon"
}
]
我想要这些
MultipleAppointments : [
{
ids :[23,35],
name : John,
email : John@domain.com,
appointment_date : tomorrow,
appointment_categories: [ 3, 5 ]
}
]
singleAppointments : [
{
id: 17,
name : “Alex",
email : “Alex@domain.com",
appointment_date : tomorrow,
appointment_category: 3,
time: “morning"
},
{
id: 22,
name : “Bob",
email : “Bob@domain.com",
appointment_date : tomorrow,
appointment_categories: 5,
time: “morning"
}
]
最佳答案
首先告诉您存储这些数据的位置。蒙戈数据库? mysql?内存?
那就换个思维方式吧。您需要知道的是哪封电子邮件发送了多于 1 个通知,而不是谁有多少次 session 。我建议您使用一个多维表,而不是 2 个一维表:
mails: {
'John@domain.com': [
{
id: 23,
name: "John",
email: "John@domain.com",
appointment_date: tomorrow,
appointment_category: 3,
time: "morning"
}, {
id: 35,
name: "John",
email: "John@domain.com",
appointment_date: tomorrow,
appointment_category: 4,
time: "afternoon"
}
]
'Alex@domain.com': [{
id: 17,
name: "Alex",
email: "Alex@domain.com",
appointment_date: tomorrow,
appointment_category: 3,
time: "morning"
}
]
'Bob@domain.com': [{
id: 22,
name: "Bob",
email: "Bob@domain.com",
appointment_date: tomorrow,
appointment_category: 5,
time: "morning"
}]
}
而且这样的东西很容易创建。
let mails = {};
AllAppointments.forEach(one=> {
if(!mails[one.email])
mails[one.email] = [];
mails[one.email].push(one)
})
for(let email in mails)
sendMail(mails[email])
关于javascript - 从其他数组中的多个对象创建对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54974198/