在JavaScript ES6中重新创建SQL查询

在JavaScript ES6中重新创建SQL查询,javascript,sql,node.js,ecmascript-6,Javascript,Sql,Node.js,Ecmascript 6,我正在尝试用JavaScript重建一个大型SQL管道,这样它就可以作为一个无服务器的CRON作业运行,而不必启动一个新的数据库。在我处理这些数据时,我的数据如下所示: const orders = [ { email: 'test@test.com', orders: '11111111', daydate: 2017-07-29, revenue: 59.99 }, { email: 'test1@test1.com', orders: '22222222', daydate

我正在尝试用JavaScript重建一个大型SQL管道,这样它就可以作为一个无服务器的CRON作业运行,而不必启动一个新的数据库。在我处理这些数据时,我的数据如下所示:

const orders = [
    { email: 'test@test.com', orders: '11111111', daydate: 2017-07-29, revenue: 59.99 },
    { email: 'test1@test1.com', orders: '22222222', daydate: 2015-07-29, revenue: 52.99 },
...
]
下面是下一个SQL查询:

SELECT
    DISTINCT(email),
    EXTRACT(month from min(daydate)) as month_acquired,
    EXTRACT(year from min(daydate)) as year_acquired
FROM orders
GROUP BY email
我想知道用JavaScript重建此查询的最佳方法是什么

一开始,我试图通过以下声明获取不同的电子邮件:

const distinctEmails = orders.filter((elem, index) => orders.findIndex(obj => obj.email === elem.email) === index)
但是这个调用花费了很长时间(我的数据集很大),并且只满足SQL查询的第一个操作

我该怎么做才好

编辑:我希望(SQL->JS)的输出如下:

[
  {email: 'test@test.com', month_acquired: 07, year_acquired: 2017},
  {email: 'test1@test1.com', month_acquired: 07, year_acquired: 2015}
]

这最终对我起了作用:

  const res = o.reduce((r, o) => {
    const key = o.email
    const monthAcquired = parseInt(o.daydate.split('-')[1])
    const yearAcquired = parseInt(o.daydate.split('-')[0])
    if (!r[key]) {
      r[key] = { email: o.email, month_acquired: monthAcquired, year_acquired: yearAcquired }
    } else {
      if (r[key].year_acquired > yearAcquired) {
        r[key].year_acquired = yearAcquired
        r[key].month_acquired = monthAcquired
      } else {
        if (r[key].year_acquired === yearAcquired && r[key].month_acquired > monthAcquired) {
          r[key].year_acquired = yearAcquired
          r[key].month_acquired = monthAcquired
        }
      }
    }
    return r
  }, {})

你能展示一下数据的结果应该是什么样的吗?
newmap(items.sort((a,b)=>b.daydate-a.daydate)。Map(v=>[v.email,v.daydate])
除了提取之外,什么都做。假设
daydate
Date
对象或时间戳。