Find the minimum item of an array by given key

Return the record with the smallest value in a field.

JavaScript Version 1.0.0 · MIT · .md

find-the-minimum-item-of-an-array-by-given-key.js
function minBy({ values, key }) { return values.reduce((best, item) => (best === null || item[key] < best[key] ? item : best), null); }

Input & expected output

Input

{
  "values": [
    {
      "recipient": "[email protected]",
      "latencyMs": 850
    },
    {
      "recipient": "[email protected]",
      "latencyMs": 410
    }
  ],
  "key": "latencyMs"
}

Expected output

{
  "recipient": "[email protected]",
  "latencyMs": 410
}

The edge case

Input

{
  "values": [],
  "key": "latencyMs"
}

Expected output

null

Know the limits

  • An empty array returns null; the original returned {}.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Find the minimum item of an array by given key from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Returns null for an empty array instead of an empty object. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.