const hasGroup = typeof Object.groupBy === typeof undefined || typeof Array.groupToMap === typeof undefined || typeof Array.group === typeof undefined; if (!hasGroup) { const groupBy = (arr, callback) => { return arr.reduce((acc = {}, ...args) => { const key = callback(...args); acc[key] ??= [] acc[key].push(args[0]); return acc; }, {}); }; if (typeof Object.groupBy === typeof undefined) { Object.groupBy = groupBy; } if (typeof Array.groupToMap === typeof undefined) { Array.groupToMap = groupBy; } if (typeof Array.group === typeof undefined) { Array.group = groupBy; } } // There are better approaches using loops directly instead of reduce // Example: console.log( Object.groupBy( [1, 2, 3, 4, 5, 6], (v) => v % 2 === 0 ? 'even': 'odd' ) );