locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
31 lines (30 loc) • 985 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.group_by = group_by;
function group_by(arr) {
// parity verified: Ruby 3.3
// discuss at: https://locutus.io/ruby/Array/group_by/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Groups array elements by identity key, mirroring `array.group_by { |v| v }`.
// example 1: group_by(['a', 'b', 'a'])
// returns 1: {a: ['a', 'a'], b: ['b']}
// example 2: group_by([1, 2, 1, 3])
// returns 2: {'1': [1, 1], '2': [2], '3': [3]}
// example 3: group_by([])
// returns 3: {}
if (!Array.isArray(arr)) {
return {};
}
const out = {};
for (const value of arr) {
const key = String(value);
const existing = out[key];
if (existing) {
existing.push(value);
}
else {
out[key] = [value];
}
}
return out;
}