cd /news/developer-tools/object-groupby-returns-a-null-protot… · home › topics › developer-tools › article
[ARTICLE · art-180936] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Object.groupBy Returns a Null-Prototype Object: Read Warehouse Groups Deliberately

A developer published a Node.js example showing that Object.groupBy returns a null-prototype object, so a warehouse group named 'toString' or '__proto__' is stored as data while groups.hasOwnProperty is undefined. The example's 14 assertions verify that Object.hasOwn correctly distinguishes existing, missing and prototype-named groups, and that grouped rows remain shared references to the original objects rather than copies. The author recommends checking the target runtime, since the local tests used Node.js 24, and agreeing on an exact warehouse-label contract before grouping.

key takeaway

Object.groupBy returns a null-prototype object, so use Object.hasOwn(groups, key) instead of groups.hasOwnProperty, which is undefined and throws a TypeError.

by read2 min views6 publishedDec 3, 2026

Object.groupBy returns a null-prototype object. A group named east can exist while groups.hasOwnProperty is unavailable. Use an own-property check that does not depend on a method inherited from Object.prototype.

This AI-assisted Node.js learning example is prepared for YX Fulfillment. It uses synthetic rows to examine a small import helper for a Shopify 3PL discussion; it does not connect to a shop, warehouse system or customer account.

MDN's Object.groupBy reference describes its return value and string-based grouping. In a drop shipping import, a warehouse label should be treated as data. A label that happens to read toString should not make the caller assume it is looking at an inherited method.

const assert = require('node:assert/strict');

function groupByWarehouse(rows) {
  if (!Array.isArray(rows)) throw new TypeError('rows must be an array');
  for (const row of rows) {
    if (!row || typeof row.warehouse !== 'string' || row.warehouse === '') {
      throw new TypeError('each row needs a warehouse string');
    }
  }
  return Object.groupBy(rows, row => row.warehouse);
}

const rows = [
  {warehouse: 'east', sku: 'A'},
  {warehouse: 'east', sku: 'B'},
  {warehouse: 'toString', sku: 'C'},
  {warehouse: '__proto__', sku: 'D'},
];
const groups = groupByWarehouse(rows);
assert.equal(Object.getPrototypeOf(groups), null);
assert.equal(Object.hasOwn(groups, 'east'), true);
assert.equal(Object.hasOwn(groups, 'missing'), false);
assert.equal(Object.hasOwn(groups, 'toString'), true);
assert.equal(groups.toString[0].sku, 'C');
assert.equal(groups.__proto__[0].sku, 'D');
assert.equal(groups.hasOwnProperty, undefined);
assert.throws(() => groups.hasOwnProperty('east'), TypeError);
assert.deepEqual(groups.east.map(row => row.sku), ['A', 'B']);
assert.equal(groups.east[0], rows[0]);
assert.deepEqual(Object.keys(groupByWarehouse([])), []);
assert.throws(() => groupByWarehouse([null]), TypeError);
assert.throws(() => groupByWarehouse(new Array(1)), TypeError);
assert.throws(() => groupByWarehouse([{warehouse: 12}]), TypeError);
console.log('devto: 14 grouping and input checks passed');

The assertions distinguish an existing group, a missing group and property names often associated with object prototypes. They also reject an absent row and a non-string label before grouping. An empty input is accepted here as an empty grouping result; it is not an instruction to approve an empty batch.

The identity assertion is intentional: the grouped row is the original object. For shopify fulfillment tooling, a view that changes a grouped row can therefore affect another view using that record. Choose a separate copying policy when isolation is required; grouping alone does not provide it.

For a china fulfillment export, agree the exact warehouse label contract before this helper runs. Converting every unexpected value to a string would hide an upstream type error. A china 3PL adapter should also keep stock ownership and release authority separate from a display grouping: this example makes neither decision.

Someone prototyping a request labelled “International Fulfillment for Folligen” could apply the same return-shape test. That phrase is a search scenario here, not evidence of a Folligen relationship or a working integration.

Run this example in an environment that provides Object.groupBy; the local checks used Node.js 24. Check the target runtime before adopting it. The useful next check is whether callers access groups through Object.hasOwn and understand that the rows remain shared references.

The photograph provides warehouse context; it does not show this code or prove that the example is deployed.

Cartons on blue pallets beside a warehouse aisle and windows.

── more in #developer-tools 4 stories · sorted by recency
── more on @node.js 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/object-groupby-retur…] indexed:0 read:2min 2026-12-03 · —