Skip to content

Instantly share code, notes, and snippets.

@ste2425
Created July 15, 2016 15:18
Show Gist options
  • Save ste2425/e70a344fb228adc2ccaa00176d0579f3 to your computer and use it in GitHub Desktop.
Save ste2425/e70a344fb228adc2ccaa00176d0579f3 to your computer and use it in GitHub Desktop.
Simple JavaScript Group
"use strict";
let group = (arr, by) => arr.reduce((prev, cur) => {
let val = isFunction(by) ? by(cur) : cur[by];
prev.hasOwnProperty(val) ? prev[val].push(cur) : prev[val] = [cur];
return prev;
}, {}),
isFunction = (x) => Object.prototype.toString.call(x) === "[object Function]";
module.exports = () => ({group, isFunction});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment