javascript - 从其他数组中的多个对象创建对象数组

标签 javascript node.js

假设我必须向列表中明天有约会的人发送电子邮件, 然而,当我编写电子邮件主题时,我想知道谁明天有多个约会,以便我可以配置主题。 我也不想一次发送多封电子邮件。我想发送一封电子邮件,说明您明天有一个约会或多个约会。 (如果知道这些约会的 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/

相关文章:

node.js - Azure BitBucket 分发失败

apache - node.js node-http-proxy 不传递 Node 模块

javascript - NodeJS 的多个 module.exports

javascript - 输入检查用户输入了 0-9 之间的值 Javascript

php - 使用 PHP、javascript 或 jQuery 使图像中的所有相对路径 URL 成为完整路径

javascript - 如何让 jQuery 自动完成在用户点击提交时执行 search()

javascript - Ajax url 参数未正确附加 - Rails 4

javascript - 在浏览器中,如何在预印纸上进行排版准确的打印?

node.js - 如何在 App-Engine 上运行的基于 Express 的 Node JS 应用程序中获取远程客户端的 IP 地址

javascript - 使用 Promise.all 时出错 : TypeError: intermediate value is not iterable at