如何按月对json对象数组进行分组? [英] How can I group an array of json objects by month?

查看:736
本文介绍了如何按月对json对象数组进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是JavaScript。我有一个JSON数组,包含以下格式的数据:

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

(上面的数字代表UTC日期/时间, p>

我想按月对数据进行分组(计数)。

  [
{Month:2014年1月,User_Count:2},
{Month:2014年2月,User_Count:1} b $ b]

我可以使用

这看起来像a map reduce 问题,高级解决方案如下:


  1. 重新组织列表中的成员。

  2. 计数列表。

这是一个逐步的方法来实现这一点:



映射




  1. 迭代字典列表

  2. 将datetime字符串转换为javascript datetime对象。

  3. 使用月 - 年作为键和字典列表作为值。

现在按月 - / p>

示例:

  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))//将f应用于l $的每个成员b $ b



减少




  1. 迭代包含列表字典的新对象。

  2. 计算每个字典列表的长度。

  3. 使用<$

例如:

  var g = function(member_count){
// extra logic may go here
return member_count
}

在o中的成员$ {
count = _.reduce(l,g(member))
member ['count'] = count
}



产生的API



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



参考文献



对于JavaScript中的映射函数,请参阅underscore.js:

http://underscorejs.org/#map

http://underscorejs.org/#reduce



JavaScript日期对象:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date



有关Date和DateTime对象的更多信息:

https://en.wikipedia.org/wiki/ISO_8601



有关 map的更多信息

code> reduce

https:/ /en.wikipedia.org/wiki/MapReduce


I'm using JavaScript. I have a JSON 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:

  1. Re-organize the members of the list.
  2. Count them.

Here is a step-by-step how-to for achieving this:

Map

  1. Iterate through the list of dictionaries
  2. Convert datetime string to javascript datetime object.
  3. 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

  1. Iterate through the new object containing dictionaries of lists.
  2. Calculate length of each dictionary's list.
  3. 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:
http://underscorejs.org/#map
http://underscorejs.org/#reduce

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

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

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

这篇关于如何按月对json对象数组进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆