
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
array-grouping-polyfill
Advanced tools
proposal-array-grouping polyfill. Polyfill for array.prototype.groupBy and array.prototype.groupByMap
Simple polyfill for tc39/proposal-array-grouping, if you dont feel like including core-js
import 'array-grouping-polyfill'
const array = [-1, 2, -4, 5, 'foo', 0, 6, 3, -3, 5];
const {negative, positive, zero, nan} = array.groupBy((i) => {
switch(true) {
case typeof i !== 'number':
return 'nan';
case i < 0:
return 'negative'
case i > 0:
return 'positive'
default:
return 'zero';
}
});
console.log(negative) // -1, -4, -3
console.log(positive) // 2, 5, 6, 3, 5
console.log(zero) // 0
console.log(nan) // foo
(Note this is a copy from tc39/proposal-array-grouping)
A proposal to make grouping of items in an array easier.
const array = [1, 2, 3, 4, 5];
// groupBy groups items by arbitrary key.
// In this case, we're grouping by even/odd keys
array.groupBy((num, index, array) => {
return num % 2 === 0 ? 'even': 'odd';
});
// => { odd: [1, 3, 5], even: [2, 4] }
// groupByToMap returns items in a Map, and is useful for grouping using
// an object key.
const odd = { odd: true };
const even = { even: true };
array.groupByToMap((num, index, array) => {
return num % 2 === 0 ? even: odd;
});
// => Map { {odd: true}: [1, 3, 5], {even: true}: [2, 4] }
Array grouping is an extremely common operation, best exemplified by
SQL's GROUP BY clause and MapReduce programming (which is better
thought of map-group-reduce). The ability to combine like data into
groups allows developers to compute higher order datasets, like the
average age of a cohort or daily LCP values for a webpage.
Two methods are offered, groupBy and groupByToMap. The first returns a
null-prototype object, which allows ergonomic destructuring and prevents
accidental collisions with global Object properties. The second returns
a regular Map instance, which allows grouping on complex key types
(imagine a compound key or [tuple]).
Just want to use a small blob of code that is stand alone?
void (function() {
var _a, _b;
var _c, _d;
(_a = (_c = Array.prototype).groupBy) !== null && _a !== void 0 ? _a : (_c.groupBy = function (callback, thisArg) {
var obj = {};
this.forEach(function (value, idx, self) {
var _a;
var ret = thisArg ? callback.call(thisArg, value, idx, self) : callback(value, idx, self);
((_a = obj[ret]) !== null && _a !== void 0 ? _a : (obj[ret] = [])).push(value);
});
return obj;
});
(_b = (_d = Array.prototype).groupByToMap) !== null && _b !== void 0 ? _b : (_d.groupByToMap = function (callback, thisArg) {
var map = new Map();
this.forEach(function (value, idx, self) {
var ret = thisArg ? callback.call(thisArg, value, idx, self) : callback(value, idx, self);
var group = map.get(ret) || [];
if (group.push(value) === 1)
map.set(ret, group);
});
return map;
});
})()
FAQs
proposal-array-grouping polyfill. Polyfill for array.prototype.groupBy and array.prototype.groupByMap
We found that array-grouping-polyfill demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.