Write groupBy(items, getKey) that groups items into an object of arrays, without using Object.groupBy.
getKey is called with each item and its index, and returns the key of the item's group. items can be any iterable. Like Object.groupBy, return an object with no prototype, so that keys like "constructor" and "__proto__" are ordinary groups.
function groupBy(items, getKey) {
// your code
}
// groupBy([1.5, 2.1, 1.9], Math.floor);
// // { 1: [1.5, 1.9], 2: [2.1] }
// groupBy(["apple", "avocado", "banana"], (word) => word[0]);
// // { a: ['apple', 'avocado'], b: ['banana'] }
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…