Created
November 1, 2019 18:32
-
-
Save haikelfazzani/9b590292911274f3778cd57d5b6e22fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var months = [ | |
"jan", "fév", "mars", "avril", "mai", "juin", "juil", "août", "sep", "oct", "nov", "déc" | |
] | |
var data = [ | |
{ user: 'kim', createdAt: '2019-10-01' }, | |
{ user: 'haikel', createdAt: '2019-11-01' }, | |
{ user: 'james', createdAt: '2019-11-01' }, | |
{ user: 'mike', createdAt: '2019-10-01' }, | |
{ user: 'joe', createdAt: '2019-09-01' }, | |
] | |
function groupBy (data, months, field) { | |
return data.reduce((a, c) => | |
(v = months[new Date(c[field]).getMonth()], a[v] ? a[v]++ : a[v] = 1, a), []); | |
} | |
let objGrouped = groupBy(data, months, 'createdAt') | |
console.log(objGrouped); // [ oct: 2, nov: 2, sep: 1 ] | |
function groupsBy (months) { | |
return Object.keys(objGrouped).map(o => { | |
return { n: objGrouped[o], m: o, indx: months.indexOf(o) } | |
}) | |
} | |
let objSorted = groupsBy(months).sort((i, j) => i.indx - j.indx) | |
console.log(objSorted); | |
// [ { n: 1, m: 'sep', indx: 8 }, { n: 2, m: 'oct', indx: 9 }, { n: 2, m: 'nov', indx: 10 } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment