最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

arrays - Javascript object group by day,month,year - Stack Overflow

matteradmin16PV0评论

I am working on application which I need to do grouping of different sets of javascript object and those will be based on month,day and year.

For day I am doing like below

var calculateByDay = function(inputList){
        var outPutList = [];    
        var result = {}
        var item = null, key = null;
        for(i=0; c<inputList.length; i++) {
           item=inputList[c];
           key = Object.keys(item)[0];
           item=item[key];
           if(!result[key]) {
                result[key] = item;
            }
           else {
            result[key] += item;
        }
        for (r in result)
            {
                var docs = {};
                docs["date"] = r;
                docs["amount"] = result[r];
                outPutList.push(docs);
            }

        }
        return outPutList;
    }

How can I improve above code and use it for month and year calculation also? I went thorough underscore.js and it has a groupBy method. but seems not fits with my requirement. I want to group by months and year also, for

var inputList = [{"2012-12-02T00:00": 2000}, {"2013-01-01T00:00": 1200},{"2013-02-02T00:00": 550}, {"2013-02-02T00:00": 1000}];

The output should be:

Monthly :
[{"December 2012": 2000}, {"January 2013": 1200},{"February 2013": 1550}];
Yearly
[{"year 2012": 2000}, {"year 2013": 2750}];

And it seems I need to this kind of map,reduce approach for large data(array sets), is there any other library or practices I can do to make the code solid?

Thanks in advance.

I am working on application which I need to do grouping of different sets of javascript object and those will be based on month,day and year.

For day I am doing like below

var calculateByDay = function(inputList){
        var outPutList = [];    
        var result = {}
        var item = null, key = null;
        for(i=0; c<inputList.length; i++) {
           item=inputList[c];
           key = Object.keys(item)[0];
           item=item[key];
           if(!result[key]) {
                result[key] = item;
            }
           else {
            result[key] += item;
        }
        for (r in result)
            {
                var docs = {};
                docs["date"] = r;
                docs["amount"] = result[r];
                outPutList.push(docs);
            }

        }
        return outPutList;
    }

How can I improve above code and use it for month and year calculation also? I went thorough underscore.js and it has a groupBy method. but seems not fits with my requirement. I want to group by months and year also, for

var inputList = [{"2012-12-02T00:00": 2000}, {"2013-01-01T00:00": 1200},{"2013-02-02T00:00": 550}, {"2013-02-02T00:00": 1000}];

The output should be:

Monthly :
[{"December 2012": 2000}, {"January 2013": 1200},{"February 2013": 1550}];
Yearly
[{"year 2012": 2000}, {"year 2013": 2750}];

And it seems I need to this kind of map,reduce approach for large data(array sets), is there any other library or practices I can do to make the code solid?

Thanks in advance.

Share Improve this question edited May 23, 2013 at 10:12 arnold asked May 22, 2013 at 20:06 arnoldarnold 1,7129 gold badges25 silver badges31 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 21

Given a slightly different structure of data:

var data = [{
  "date": "2011-12-02T00:00",
  "value": 1000
}, {
  "date": "2013-03-02T00:00",
  "value": 1000
}, {
  "date": "2013-03-02T00:00",
  "value": 500
}, {
  "date": "2012-12-02T00:00",
  "value": 200
}, {
  "date": "2013-04-02T00:00",
  "value": 200
}, {
  "date": "2013-04-02T00:00",
  "value": 500
}, {
  "date": "2013-03-02T00:00",
  "value": 500
}, {
  "date": "2013-04-12T00:00",
  "value": 1000
}, {
  "date": "2012-11-02T00:00",
  "value": 600
}];

You could use underscore:

var grouped = _.groupBy(data, function(item) {
    return item.date;
});

var groupedByYear = _.groupBy(data, function(item) {
    return item.date.substring(0,4);
});

var groupedByMonth = _.groupBy(data, function(item) {
    return item.date.substring(0,7);
});

console.log(groupedByYear);

See related answer: Javascript - underscorejs map reduce groupby based on date

Please see if the following refactor is useful for you http://jsfiddle.net/wkUJC/

var dates = [{"2012-12-02T00:00": 2000}, {"2013-01-01T00:00": 1200},{"2013-02-02T00:00": 550}, {"2013-02-02T00:00": 1000}];

function calc(dates) {
    var response = {};
    dates.forEach(function(d){
        for (var k in d) {
            var _ = k.split("-");
            var year = _[0]
            var month = _[1]
            if (!response[year]) response[year] = {total: 0}
            response[year][month] = response[year][month] ? response[year][month]+d[k] : d[k]
            response[year].total+= d[k]
        }
    });
    console.log(response);
    return response;
}

calc(dates);
Post a comment

comment list (0)

  1. No comments so far