Skip to content

Instantly share code, notes, and snippets.

@dmorenogogoleva
Created June 6, 2021 11:09
Show Gist options
  • Save dmorenogogoleva/11ce687445f9f78ff67da6ffe7c8dc9e to your computer and use it in GitHub Desktop.
Save dmorenogogoleva/11ce687445f9f78ff67da6ffe7c8dc9e to your computer and use it in GitHub Desktop.
custom groupBy
export function groupBy(collection: any[], iteratee: string) {
return collection.reduce((result, value) => {
if (!value.hasOwnProperty(iteratee)) return result
const key = value[iteratee]
if (!result || !result[key]) {
result[key] = [value]
return result
}
result[key] = [...result[key], value]
return result
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment