2
votes

I Have 10 years of records. so API will return the array I need to group the array based on date time stamp. A user can choose a grouping option as

  • hour
  • 6 hours(per day)
  • day
  • week
  • month
  • year

data example

[{
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.18809978382007495
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.42347719862654404
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.365386421616013
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.49364744650351033
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.31133576985952016
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5509062071104307
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5556739085429424
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6668348570127745
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.4908101365372941
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6042127351503115
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.3725497529313371
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.29687095332790064
}, {
  "x": "2018-02-07T11:19:19.034Z",
  "y": 0
}]

Is there any generic solutions to get grouping by hour/day/week/month/year

Example output what I want.

for hour

{
 "OCT 2th 2012 6AM": [
    "2012-10-02 06:00:00",
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

for every 6 hour

{
 "OCT 6th 2012 first 6 hours": [
    "2012-10-11 06:00:00",
  ],
 "OCT 6th 2012 second 6 hours": [
    "2012-10-10 06:03:51"
  ],
  "NOV 6th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-111-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

for day

{
 "OCT 2th 2012": [
    "2012-10-2 06:00:00",
  ],
 "OCT 10th  2012": [
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 7th 2013": [
    "2013-10-07 02:07:02"
  ],
}

.for week

{
 "OCT 2012 week number": [
    "2012-10-2 06:00:00",
  ],
 "OCT 2012 week number": [
    "2012-10-10 06:03:51"
  ],

}

for month

{
 "OCT 2012 ": [
    "2012-10-2 06:00:00",
  ],
 "NOV 2012": [
    "2012-11-10 06:03:51"
  ],

}
1
can you please provide sample output what you are expected if possible.vinodsesetti
Do you want to group hour in day and day in week and month in year like nested grouping? So something like {2009:{Jan:{2:21:{8:[{item}]}}}}HMR
If you don't need nested groups then lodash groupBy takes a function as second argument: _.groupBy(data,item=>new Date(item.x).getFullYear()) will group by year. You can make it dynamic with: const currentGroup='year';const groups = {year:item=>new Date(item.x).getFullYear()};_.groupBy(data,groups[currentGroup])HMR
@HMR. I have updated my question please checkJagadeesh Govindaraj
What is it you need to know, how to write the functions that will return the right key from a date or what way to code a general group method?HMR

1 Answers

2
votes

You can do it the following way maybe?

const data = [{"x":"2019-02-05T14:28:45.540Z","y":0.18809978382007495},{"x":"2019-02-05T14:28:45.540Z","y":0.42347719862654404},{"x":"2019-02-05T14:28:45.540Z","y":0.365386421616013},{"x":"2019-02-05T14:28:45.540Z","y":0.49364744650351033},{"x":"2019-02-05T14:28:45.540Z","y":0.31133576985952016},{"x":"2019-02-05T14:28:45.540Z","y":0.5509062071104307},{"x":"2019-02-05T14:28:45.540Z","y":0.5556739085429424},{"x":"2019-02-05T14:28:45.540Z","y":0.6668348570127745},{"x":"2019-02-05T14:28:45.540Z","y":0.4908101365372941},{"x":"2019-02-05T14:28:45.540Z","y":0.6042127351503115},{"x":"2019-02-05T14:28:45.540Z","y":0.3725497529313371},{"x":"2019-02-05T14:28:45.540Z","y":0.29687095332790064},{"x":"2018-02-07T11:19:19.034Z","y":0}];

const groups = (() => {
    const byDay = (item) => moment(item.x).format('MMM DD YYYY'),
        forHour = (item) => byDay(item) + ' ' + moment(item.x).format('hh a'),
        by6Hour = (item) => {
            const m = moment(item.x);
            return byDay(item) + ' ' + ['first', 'second', 'third', 'fourth'][Number(m.format('k')) % 6] + ' 6 hours';
        },
        forMonth = (item) => moment(item.x).format('MMM YYYY'),
        forWeek = (item) => forMonth(item) + ' ' + moment(item.x).format('ww');
    return {
        byDay,
        forHour,
        by6Hour,
        forMonth,
        forWeek,
    };
})();

const currentGroup = 'forWeek';
console.log(_.groupBy(data, groups[currentGroup]));
<script src="https://lodash.com/vendor/cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://momentjs.com/static/js/global.js"></script>