Last active
February 9, 2017 16:32
-
-
Save oakley808/4f99e5c1bc18d6507786be4bb3b3c2dc 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
let groupBy = 'date'; | |
let apiData = [{date: 3, count: 5},{date: 2, count: 3},{date: 1, count: 1},{date: 1, count: 2},{date: 2, count: 4},{date: 3, count: 6}]; | |
let sortedApiData = apiData.sort((a, b) => (a[groupBy] > b[groupBy]) ? 1 : ((b[groupBy] > a[groupBy]) ? -1 : 0 )) | |
let dateArray = sortedApiData.map((el, idx) => el[groupBy]).filter((el, i, a) => el !== a[i-1]); | |
result = dateArray | |
.map((dayNumber, idx) => | |
sortedApiData | |
.filter((apiDataPoint, i) => | |
apiDataPoint[groupBy] === dayNumber | |
) | |
) // array of arrays clustered by date | |
.map((dateGroup, idx) => | |
dateGroup | |
.reduce((accumulator, current) => ({ | |
date: accumulator.date, | |
count: accumulator.count + current.count | |
}) | |
) | |
) // array of objects reduced by count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment