_.groupBy momentjs格式返回键未定义

时间:2018-02-28 22:31:01

标签: javascript group-by lodash

enter image description here

我尝试按日期对这些记录进行分组,例如:2018/02/28

我的代码使用的是lodash的_.groupBy函数。

let groupedData = _.groupBy(data, access_log => {
    moment
      .utc(access_log.last_access_at)
      .local()
      .format("YYYY-MM-DD");
  });

这会使分组数据返回正常,但该组的密钥为undefined。在给定格式匹配器的情况下,不应该将日期作为"2018/02/28返回吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

你没有归还任何东西。只需在xs之前添加return由于您在胖箭头功能中添加了 {} ,您需要指定返回关键字,否则删除 {}

moment.utc(access_log.last_access_at) .local() .format("YYYY-MM-DD");

答案 1 :(得分:0)

据我看到您的代码缺少return语句,请尝试

static void Main(string[] args)
{
    String username = "set me";
    String apiKey = "set me";
    int invoiceId = 123456;

    authenticate authenticate = new authenticate();
    authenticate.username = username;
    authenticate.apiKey = apiKey;

    // Initialize the SoftLayer_Account API service.
    SoftLayer_Billing_InvoiceService invoiceService = new SoftLayer_Billing_InvoiceService();
    invoiceService.authenticateValue = authenticate;

    SoftLayer_Billing_InvoiceInitParameters billingInvoiceInitParameters = new SoftLayer_Billing_InvoiceInitParameters();
    billingInvoiceInitParameters.id = invoiceId;
    invoiceService.SoftLayer_Billing_InvoiceInitParametersValue = billingInvoiceInitParameters;

    SoftLayer_Billing_Invoice result = invoiceService.getObject();
    Console.WriteLine(result);
}

let groupedData = _.groupBy(data, access_log => (moment
      .utc(access_log.last_access_at)
      .local()
      .format("YYYY-MM-DD")
  ));