summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/common/util.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/public/common/util.js b/public/common/util.js
index 75cd824..0979178 100644
--- a/public/common/util.js
+++ b/public/common/util.js
@@ -304,3 +304,26 @@ function object_diff(a, b) {
}
return true
}
+
+// same as Object.groupBy
+function object_group_by(items, callback) {
+ let groups = {}
+ if (typeof callback === "function") {
+ for (let item of items) {
+ let key = callback(item)
+ if (key in groups)
+ groups[key].push(item)
+ else
+ groups[key] = [ item ]
+ }
+ } else {
+ for (let item of items) {
+ let key = item[callback]
+ if (key in groups)
+ groups[key].push(item)
+ else
+ groups[key] = [ item ]
+ }
+ }
+ return groups
+}