简体   繁体   English

GroupBy 与 LinqTS(打字稿)

[英]GroupBy with LinqTS(Typescript)

I am using LINQTS to have some information from via grouping some data.我正在使用 LINQTS 通过对一些数据进行分组来获取一些信息。

 const registered_users_data = listData
  .groupBy(
    b => moment(b.created_at).format('YYYY/MM')
  )
  .select(x => {
    x.key, x.value.count();
  })
  .toArray();

For above code I am getting below response highlighted in red area对于上面的代码,我得到以下红色区域突出显示的响应

在此处输入图像描述

If I change the code to below如果我将代码更改为以下

listData
  .groupBy(b => moment(b.created_at).format('YYYY/MM'))
  .select(x => x.value.count())
  .toArray();

then I get below result然后我得到以下结果

在此处输入图像描述

I want to have both key and value count in response.我希望同时计算键和值作为响应。 Can anybody suggest what I am doing wrong in above code where I am getting null in array.任何人都可以建议我在上面的代码中做错了什么,我在数组中得到 null。

The curly brackets in x => { x.key, x.value.count(); } x => { x.key, x.value.count(); }中的大括号x => { x.key, x.value.count(); } doesn't create an object when used in an arrow function because they are used to create a sequence of statements which should end with a return (if no return is provided, the value will be null, as in your case).在箭头x => { x.key, x.value.count(); }中使用时不会创建 object,因为它们用于创建应以return结尾的语句序列(如果未提供return ,则在您的情况下,该值将为 Z37A6259CC0C1DAE299A786648)DFF。 What you can do is wrap the object in parantheses.您可以做的是将 object 包装在括号中。 See the MDN documentation请参阅MDN 文档

So this is what the code should look like:所以这就是代码的样子:

const registered_users_data = listData
  .groupBy(
    b => moment(b.created_at).format('YYYY/MM')
  )
  .select(x => ({
    key: x.key,
    value: x.value.count();
  }))
  .toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM