Array.prototype.reduce Polyfill
Implement Array.prototype.reduce from scratch, including the tricky no-initial-value path and the empty-array error case.
JavaScript · Arrays · Polyfills — Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
Add myGroupBy(fn) to Array.prototype.
fn returns the key for each item. The result is an object whose values are arrays of the items sharing that key, in their original order.
Input:
JSfile.javascript1[1, 2, 3].myGroupBy((n) => (n % 2 ? 'odd' : 'even'));
Output:
{ odd: [1, 3], even: [2] }
Each item is appended to the bucket its key names.
out[key] = out[key] || [] breaks when a key is "constructor" or "toString", which are truthy on every object.Grouping is the operation reduce was made for, and it is now a real language feature: Object.groupBy and Map.groupBy ship in modern browsers. Implementing it by hand is still asked because the failure mode is interesting.
For each item, compute the key, ensure a bucket, push.
JSfile.javascript1Array.prototype.myGroupBy = function (fn) { 2 const out = {}; 3 for (let i = 0; i < this.length; i += 1) { 4 const key = fn(this[i], i, this); 5 if (!Object.prototype.hasOwnProperty.call(out, key)) out[key] = []; 6 out[key].push(this[i]); 7 } 8 return out; 9};
out[key] = out[key] || [] looks correct and is not. Every plain object inherits constructor, toString and valueOf from its prototype, and all of them are truthy — so grouping by a key that happens to be "constructor" finds a truthy value and calls .push on a function. hasOwnProperty avoids it, and Object.create(null) avoids it structurally by making an object with no prototype at all.
Order inside a bucket must match the input, so walk forwards and append.
Goal: Bucket every item under its key, preserving order inside each bucket.
constructor, toString and valueOf, and all of them are truthy. Grouping by a key called "constructor" finds an inherited function and tries to push onto it.Object.create(null) for the result, which creates an object with no prototype and therefore no inherited keys, or guard every lookup with Object.prototype.hasOwnProperty.call.Object.groupBy and Map.groupBy are available in current browsers and Node. Map.groupBy is the better choice when keys are not strings, since object keys are always coerced to strings.Continue learning with these related challenges
Implement Array.prototype.reduce from scratch, including the tricky no-initial-value path and the empty-array error case.
JavaScript · Arrays · Polyfills — Pratik Rai ·
Implement Object.assign from scratch: copy own enumerable properties (including Symbols) from sources onto the target.
JavaScript · Objects · Polyfills — Pratik Rai ·
Implement Object.create using the classic empty-constructor trick to set an object's prototype chain.
JavaScript · Objects · Prototypes — Pratik Rai ·
Implement Array.prototype.reduce from scratch, including the tricky no-initial-value path and the empty-array error case.
JavaScript · Arrays · Polyfills
Pratik Rai ·
Implement Object.assign from scratch: copy own enumerable properties (including Symbols) from sources onto the target.
JavaScript · Objects · Polyfills
Pratik Rai ·
Implement Object.create using the classic empty-constructor trick to set an object's prototype chain.
JavaScript · Objects · Prototypes
Pratik Rai ·