Last active
April 7, 2018 03:33
-
-
Save hiepxanh/e24f63e9f57aefcb34241671de1cf3c9 to your computer and use it in GitHub Desktop.
group by code, turn array order buy property
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
declare global { | |
interface Array<T> { | |
groupBy(elem: T): Array<T>; | |
} | |
} | |
Array.prototype.groupBy = function(prop) { | |
return this.reduce(function(groups, item) { | |
const val = item[prop] | |
groups[val] = groups[val] || [] | |
groups[val].push(item) | |
return groups | |
}, {}) | |
} | |
// ussing | |
news = news.groupBy('localeDate'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment