如何在JS地图功能中分组

发布于 2025-02-13 20:08:36 字数 916 浏览 0 评论 0原文

我有一些来自DB的数据:

{InteractionDate: '2022-07-07', Time: 10, ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'}
{InteractionDate: '2022-07-07', Time: 20, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'}
{InteractionDate: '2022-07-07', Time: 33, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'}
{InteractionDate: '2022-07-07', Time: 30, ID: '229530ed-ca44-4015-b1d7-b528be5b6071'}

我需要在UI中显示它:

Month   Time    # of Unique ID's
=====   ====    ================
07/22   93      3

ID是我需要一些帮助的新增加。现在,我正在使用它来分组所有日期并总结时间。

        const groupedArray = _(allInteractions)
        .groupBy(v => moment(v.InteractionDate).format('MM/YYYY'))
        .map((item, date) => ({
            interactionDate: date,
            totalSec: _.sumBy(item, 'Time')
            //uniqueIDs:    ?
        }))
        .value();

问题是 - 我怎么知道07/22中看到了多少个不同的ID?

I have some data coming from the DB:

{InteractionDate: '2022-07-07', Time: 10, ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'}
{InteractionDate: '2022-07-07', Time: 20, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'}
{InteractionDate: '2022-07-07', Time: 33, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'}
{InteractionDate: '2022-07-07', Time: 30, ID: '229530ed-ca44-4015-b1d7-b528be5b6071'}

and I need to show it in the UI like so:

Month   Time    # of Unique ID's
=====   ====    ================
07/22   93      3

The ID's is a new addition that I need some help with. Right now, I'm using this to group all the dates and sum the times.

        const groupedArray = _(allInteractions)
        .groupBy(v => moment(v.InteractionDate).format('MM/YYYY'))
        .map((item, date) => ({
            interactionDate: date,
            totalSec: _.sumBy(item, 'Time')
            //uniqueIDs:    ?
        }))
        .value();

The question is - How do I know how many DIFFERENT id's were seen in 07/22?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2025-02-20 20:08:37

我的解决方案不是那么漂亮,但是它正在起作用

var allInteractions = get_data();
const groupedArray = _(allInteractions)
  .groupBy(v => moment(v.InteractionDate).format('MM/YYYY'))
  .map(function(item, date) {
    var obj = {}
    item.forEach(function(item) {
      obj[item.ID] = true
    });
    return {
      interactionDate: date,
      totalSec: _.sumBy(item, 'Time'),
      uniqueIDs: Object.keys(obj).length
    }
  })
  .value();

console.log(groupedArray)

function get_data() {
  return [{
      InteractionDate: '2022-07-07',
      Time: 10,
      ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 20,
      ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 33,
      ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 30,
      ID: '229530ed-ca44-4015-b1d7-b528be5b6071'
    },
    {
      InteractionDate: '2022-05-05',
      Time: 30,
      ID: '229530ed-ca44-4015-b1d7-b528be5b6071'
    }
  ];

}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

My solution is not that pretty, but it's working

var allInteractions = get_data();
const groupedArray = _(allInteractions)
  .groupBy(v => moment(v.InteractionDate).format('MM/YYYY'))
  .map(function(item, date) {
    var obj = {}
    item.forEach(function(item) {
      obj[item.ID] = true
    });
    return {
      interactionDate: date,
      totalSec: _.sumBy(item, 'Time'),
      uniqueIDs: Object.keys(obj).length
    }
  })
  .value();

console.log(groupedArray)

function get_data() {
  return [{
      InteractionDate: '2022-07-07',
      Time: 10,
      ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 20,
      ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 33,
      ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'
    },
    {
      InteractionDate: '2022-07-07',
      Time: 30,
      ID: '229530ed-ca44-4015-b1d7-b528be5b6071'
    },
    {
      InteractionDate: '2022-05-05',
      Time: 30,
      ID: '229530ed-ca44-4015-b1d7-b528be5b6071'
    }
  ];

}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

我最亲爱的 2025-02-20 20:08:37

这是使用内置set运算符进行的香草JS方法。

const data = [{InteractionDate: '2022-07-07', Time: 10, ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'},
{InteractionDate: '2022-07-07', Time: 20, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'},
{InteractionDate: '2022-07-07', Time: 33, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'},
{InteractionDate: '2022-07-07', Time: 30, ID: '229530ed-ca44-4015-b1d7-b528be5b6071'}]

const result = data.reduce((acc, el) => {
  if (!acc[el.InteractionDate]) {
    acc[el.InteractionDate] = { time: 0, ids: new Set() }
  }
  acc[el.InteractionDate].time += el.Time;
  acc[el.InteractionDate].ids.add(el.ID);
  return acc;
}, {});

Object.entries(result).forEach(([date, { time, ids }]) => {
  console.log({date, time, ids: ids.size });
})

Here is a vanilla JS way to do it using the built-in Set operator.

const data = [{InteractionDate: '2022-07-07', Time: 10, ID: 'eac61b7e-8e25-4445-ad1d-f1ee8c6ac86c'},
{InteractionDate: '2022-07-07', Time: 20, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'},
{InteractionDate: '2022-07-07', Time: 33, ID: 'c3a78dbe-553d-4ecc-b4ef-8d8d3039c521'},
{InteractionDate: '2022-07-07', Time: 30, ID: '229530ed-ca44-4015-b1d7-b528be5b6071'}]

const result = data.reduce((acc, el) => {
  if (!acc[el.InteractionDate]) {
    acc[el.InteractionDate] = { time: 0, ids: new Set() }
  }
  acc[el.InteractionDate].time += el.Time;
  acc[el.InteractionDate].ids.add(el.ID);
  return acc;
}, {});

Object.entries(result).forEach(([date, { time, ids }]) => {
  console.log({date, time, ids: ids.size });
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文