最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript - underscorejs map reduce groupby based on date - Stack Overflow

programmeradmin1浏览0评论

I am working with underscorejs in a project. I need to do different types of array operations like map,reduce, filter groupby.

My dataset looks like below:

var data_list = [
     {"insertDate": "2011-12-02T00:00","value": {"paisa":10000}},
    {"insertDate": "2013-03-02T00:00","value": {"paisa":10000}
    },{"insertDate": "2013-03-02T00:00","value": {"paisa":10000}
    },{"insertDate": "2012-12-02T00:00","value": {"paisa":30000}},
  {"insertDate": "2013-04-02T00:00","value": {"paisa":10000},
    },{"insertDate": "2013-04-02T00:00","value": {"paisa":30000}
    },{"insertDate": "2013-03-02T00:00","value": {"paisa":20000}},
  {"insertDate": "2013-04-12T00:00","value": {"paisa":30000}
    },
    {"insertDate": "2012-11-02T00:00","value": {"paisa":10000}}
]

I want to calculated data based on day, month and year. For day calculation I am doing groupby, then using map reduce for calculation. Like below:

var groupedByDateData = _.groupBy(data_list, "insertDate");
var aggregateByDate = _.map(groupedByDateData, function(invoiceObject, createdat) {
  return { 
     createdat: createdat, 
     val: _.reduce(invoiceObject, function(m,x) { 
       return m + x.value.paisa;
     }, 0) 
  };
});

Its working fine for day based map reduce, but for month and year calculation I want to do in same format as the code readable.

How can I do map,reduce using underscorejs so that I can get data like below: For monthly calculation:

[{"createdat": "February 2011", "val": 10000}, 
{"createdat": "March 2013", "val": 40000}, 
{"createdat": "December 2012", "val": 30000}, 
{"createdat": "April 2013", "val": 70000}, 
{"createdat": "November 2012", "val": 10000}]

For yearly mapreduce:

[{"createdat": "2011", "val": 10000}, 
{"createdat": "2013", "val": 110000}, 
{"createdat": "2012", "val": 70000}]

My code JSBIN here

N.B: For getting yearly and monthly reduced result set I am splitting the date, but that is not best thing to do. check out my solution here

Thanks in advance.

I am working with underscorejs in a project. I need to do different types of array operations like map,reduce, filter groupby.

My dataset looks like below:

var data_list = [
     {"insertDate": "2011-12-02T00:00","value": {"paisa":10000}},
    {"insertDate": "2013-03-02T00:00","value": {"paisa":10000}
    },{"insertDate": "2013-03-02T00:00","value": {"paisa":10000}
    },{"insertDate": "2012-12-02T00:00","value": {"paisa":30000}},
  {"insertDate": "2013-04-02T00:00","value": {"paisa":10000},
    },{"insertDate": "2013-04-02T00:00","value": {"paisa":30000}
    },{"insertDate": "2013-03-02T00:00","value": {"paisa":20000}},
  {"insertDate": "2013-04-12T00:00","value": {"paisa":30000}
    },
    {"insertDate": "2012-11-02T00:00","value": {"paisa":10000}}
]

I want to calculated data based on day, month and year. For day calculation I am doing groupby, then using map reduce for calculation. Like below:

var groupedByDateData = _.groupBy(data_list, "insertDate");
var aggregateByDate = _.map(groupedByDateData, function(invoiceObject, createdat) {
  return { 
     createdat: createdat, 
     val: _.reduce(invoiceObject, function(m,x) { 
       return m + x.value.paisa;
     }, 0) 
  };
});

Its working fine for day based map reduce, but for month and year calculation I want to do in same format as the code readable.

How can I do map,reduce using underscorejs so that I can get data like below: For monthly calculation:

[{"createdat": "February 2011", "val": 10000}, 
{"createdat": "March 2013", "val": 40000}, 
{"createdat": "December 2012", "val": 30000}, 
{"createdat": "April 2013", "val": 70000}, 
{"createdat": "November 2012", "val": 10000}]

For yearly mapreduce:

[{"createdat": "2011", "val": 10000}, 
{"createdat": "2013", "val": 110000}, 
{"createdat": "2012", "val": 70000}]

My code JSBIN here

N.B: For getting yearly and monthly reduced result set I am splitting the date, but that is not best thing to do. check out my solution here http://jsbin./etepan/3/edit

Thanks in advance.

Share Improve this question edited May 26, 2013 at 12:22 mushfiq asked May 24, 2013 at 21:03 mushfiqmushfiq 1,6002 gold badges19 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You'll want to be grouping by different things (time, month, year), so the key is in the groupBy.

You're using this:

var groupedByDateData = _.groupBy(data_list, "insertDate");

Which is shorthand for:

var groupedByDateData = _.groupBy(data_list, function(item) {
  return item.insertDate; // returns "YYYY-MM-DDT00:00"
});

By modifying this function, you can change what groupings you get. For example, year:

var groupedByDateData = _.groupBy(data_list, function(item) {
  // pretty crappy way of getting the year,
  // but luckily the dates are structured
  return item.insertDate.substring(0,4); 
});

// => [
//      {"createdat": "2011", "val": 10000},
//      {"createdat": "2012", "val": 40000},
//      {"createdat": "2013", "val": 110000}
//    ]

Month:

var groupedByDateData = _.groupBy(data_list, function(item) {
  return item.insertDate.substring(0,7); 
});

// => [
//      {"createdat": "2011-12", "val": 10000},
//      {"createdat": "2013-03", "val": 40000},
//      {"createdat": "2012-12", "val": 30000},
//      {"createdat": "2013-04", "val": 70000},
//      {"createdat": "2012-11", "val": 10000}
//    ]

Your JS Bin helped a lot: http://jsbin./ucohoq/7/edit

发布评论

评论列表(0)

  1. 暂无评论