Prerequisites and grouping contract

Grouping records by an external string is common in reports. A plain object accumulator can interact awkwardly with property names such as constructor and __proto__. This example uses Map so team names are keys in a collection rather than properties on an object prototype chain. Run the complete fixture with a current Node.js runtime. The grouping function itself uses standard JavaScript and has no dependency on a UI framework.

The contract requires each input record to have its own string-valued team property. Missing properties, inherited team values, arrays and null are rejected. Empty strings are valid in this example because the policy only requires a string. If your application requires nonempty or normalized names, enforce that rule before inserting the key.

Follow insertion and member order

The loop creates one array when a team is first seen and appends each record to that array. Map iteration retains first insertion order for these keys. In the fixture, __proto__ appears first, support second and constructor third. The second __proto__ record joins the existing group and does not move that group to the end.

The assertions verify both the key order and the order of member identifiers inside the first group. A grouped report often needs these as separate promises. If your presentation requires alphabetical group order, sort an exported array explicitly; do not assume that grouping also performs sorting. Likewise, reorder member arrays only when the report specification calls for it.

Object ownership and serialization

The grouping function keeps references to the original records. It creates fresh arrays for membership but does not copy the record objects. The identity assertion documents that choice. If a caller later changes a record, the group will see that same changed object; snapshot the fields you need before grouping when historical report values must remain fixed.

The summary is exported as an array of objects with team and count fields. That shape makes ordering visible and can be serialized with JSON.stringify. Serializing a Map directly would not produce this keyed report representation. Avoid converting untrusted team names into properties on a normal object merely to prepare a response when an array already expresses the required data.

Troubleshooting and edge cases

Object.hasOwn avoids calling a possibly shadowed hasOwnProperty method from the record. The validation fixture includes a record with an inherited team property and verifies rejection. This is input-shape checking for ordinary parsed records, not a sandbox for arbitrary JavaScript objects with getters or proxies.

An empty input produces an empty Map. Duplicate team names create more members, not more groups. Visually similar Unicode spellings or different case remain distinct keys because this code does no text normalization. When apparently identical groups appear twice, inspect the exact input strings before adding a normalization policy that might combine genuinely different names. All executed tests were local fixtures; no browser rendering or external report source was exercised.

Complete example

Save as group.cjs.

const assert = require('node:assert/strict');
function groupByTeam(rows) {
  const groups = new Map();
  for (const row of rows) {
    if (row === null || typeof row !== 'object' || Array.isArray(row)
        || !Object.hasOwn(row, 'team') || typeof row.team !== 'string')
      throw new TypeError('each record needs its own string team');
    if (!groups.has(row.team)) groups.set(row.team, []);
    groups.get(row.team).push(row);
  }
  return groups;
}
const rows = [
  {team:'__proto__', id:1}, {team:'support', id:2},
  {team:'constructor', id:3}, {team:'__proto__', id:4}
];
const groups = groupByTeam(rows);
assert.deepEqual([...groups.keys()], ['__proto__','support','constructor']);
assert.deepEqual(groups.get('__proto__').map(row => row.id), [1,4]);
assert.equal(groupByTeam([]).size, 0);
assert.equal(groupByTeam([{team:''}]).has(''), true);
assert.equal(groups.get('support')[0], rows[1]);
for (const bad of [null, [], {}, {team:7}, Object.create({team:'inherited'})])
  assert.throws(() => groupByTeam([bad]), TypeError);
const summary = [...groups].map(([team, members]) => ({team, count:members.length}));
console.log(JSON.stringify(summary));

Run and expected output

node group.cjs
[{"team":"__proto__","count":2},{"team":"support","count":1},{"team":"constructor","count":1}]

Primary references

Continue with Javascript tutorials.