diff options
author | Tor Andersson <tor@ccxvii.net> | 2024-03-14 17:18:39 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2024-03-14 23:44:33 +0100 |
commit | 0bfc37790c286c4a8a979a6ace15171340d45be5 (patch) | |
tree | 811913f1d27ea5c8fcb61a903e8175309770ca23 | |
parent | f4af4c591ff3c1a50b1a6ce28acbd298fc80fdfa (diff) | |
download | server-0bfc37790c286c4a8a979a6ace15171340d45be5.tar.gz |
Add Object.groupBy implementation to common utils.
-rw-r--r-- | public/common/util.js | 23 |
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 +} |