如何按月对一组对象进行分组?

编程入门 行业动态 更新时间:2024-10-15 06:16:58
本文介绍了如何按月对一组对象进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用JavaScript。我有一个包含这种格式数据的数组:

[ {USER_NAME:User1, LAST_SUCCESSFUL_CONNECT:1373978337642, {USER_NAME:User2,LAST_SUCCESSFUL_CONNECT:1374515704026}, {USER_NAME:User3,LAST_SUCCESSFUL_CONNECT:1374749782479 (上面的数字表示以毫秒为单位的UTC日期/时间。)

我想按月对数据进行分组(计数),如下所示:

我可以使用 jQuery 如果它简化了一些事情。

map reduce 问题。高级解决方案如下:

  • 重新组织这个memb
  • $ b

    以下是一步一步的介绍如何实现这一点: Map

  • 遍历字典列表
  • 将datetime字符串转换为javascript datetime对象。
  • 使用month-year作为键和词典列表作为值。
  • 现在按月份分组。

    例子:

    var l = [...]; var o = {}; var f = function(x){ var dt_object = Date(x [LAST_SUCCESSFUL_CONNECT]); //转换为datetime对象 var key = dt_object.year +' - '+ dt_object.month; if(o [key] === undefined){ var o [key] = []; }; $ b $ [b] bush(x)} _.map(l,f(x))//将f应用于l

    减少

  • 迭代包含列表字典的新对象。
  • 计算每个字典列表的长度。
  • 使用 count 作为键值和列表长度作为其值。
  • 示例:

    var g = function(member_count){ //额外的逻辑可能会在这里返回member_count } 用于o { count = _.reduce(l,g(成员))成员['count'] = count }

    结果API

    o ['month-year'] //当月字典清单o ['month-year'] ['count'] //用于该月字典列表的计数。

    参考文献:

    在javascript中为 map 和 reduce 函数请参阅underscore.js: underscorejs/#map underscorejs/#reduce

    Javascript日期对象: developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    有关Date和DateTime对象的更多信息: en.wikipedia/wiki/ISO_8601

    有关 map 的更多信息, code> reduce : https:/ /en.wikipedia/wiki/MapReduce

    I'm using JavaScript. I have an array that contains data in this format:

    [ {"USER_NAME":"User1","LAST_SUCCESSFUL_CONNECT":"1373978337642"}, {"USER_NAME":"User2","LAST_SUCCESSFUL_CONNECT":"1374515704026"}, {"USER_NAME":"User3","LAST_SUCCESSFUL_CONNECT":"1374749782479"} ]

    (the numbers above represent UTC date/time in milliseconds.

    I would like to group (count) the data by month. Something like this:

    [ {"Month":"January, 2014","User_Count": 2}, {"Month":"February, 2014","User_Count": 1}, ]

    I could use jQuery if it simplifies matters.

    解决方案

    This looks like a map reduce problem. The high-level solution is as follows:

  • Re-organize the members of the list.
  • Count them.
  • Here is a step-by-step how-to for achieving this:

    Map

  • Iterate through the list of dictionaries
  • Convert datetime string to javascript datetime object.
  • Use month-year as key and list of dictionaries as value.
  • These are now grouped by month-year.

    Example:

    var l = [...]; var o = {}; var f = function(x){ var dt_object = Date(x["LAST_SUCCESSFUL_CONNECT"]); // convert to datetime object var key = dt_object.year + '-' + dt_object.month; if (o[key] === undefined) { var o[key] = []; }; o[key].push(x) } _.map(l, f(x)) //apply f to each member of l

    Reduce

  • Iterate through the new object containing dictionaries of lists.
  • Calculate length of each dictionary's list.
  • Use count as key and length of list as its value.
  • Example:

    var g = function(member_count){ //extra logic may go here return member_count } for member in o { count = _.reduce(l, g(member)) member['count'] = count }

    Resulting API

    o['month-year'] //for list of dictionaries in that month o['month-year']['count'] //for the count of list of dictionaries in that month.

    References:

    For map and reduce functions in javascript see underscore.js: underscorejs/#map underscorejs/#reduce

    Javascript Date object: developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    For more information on Date and DateTime objects: en.wikipedia/wiki/ISO_8601

    For more information on map reduce: en.wikipedia/wiki/MapReduce

    更多推荐

    如何按月对一组对象进行分组?

    本文发布于:2023-05-23 15:05:40,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:按月   对象

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!