Array Of JS Dates How To Group By Days

前端 未结 4 1454
旧时难觅i
旧时难觅i 2020-12-05 18:10

I\'m trying to figure out the most optimal and with as minimum amount of loops way to group my array of js dates objects from this: (Take a note this is browser console outp

4条回答
  •  有刺的猬
    2020-12-05 18:22

    If you need to grouping also by year or (and) month with day - I recommend to use my solution.

    In answers above if you'll get different month or year with the same day - your grouping will be incorrect.

    Look at the good solution:

    _.groupBy(arrayOfDates, function (el) {
          return (el.getFullYear() + '|y|') + (el.getMonth() + '|m|') + (el.getDate() + '|d|');
        });
    

    What I do here? Just create an unique keys for each date, which includes: year, month and day. And then I group an array by this unique key.

    result

提交回复
热议问题