# Count by the properties of an array of objects

Tally records by the value of one field.

Canonical: https://1loc.dev/array/count-by-the-properties-of-an-array-of-objects/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function countBy({ values, key }) { return { ...values.reduce((counts, item) => ((counts[item[key]] = (counts[item[key]] || 0) + 1), counts), Object.create(null)) }; }
```

## Input

```json
{
  "values": [
    {
      "status": "delivered"
    },
    {
      "status": "deferred"
    },
    {
      "status": "delivered"
    }
  ],
  "key": "status"
}
```

## Expected output

```json
{
  "delivered": 2,
  "deferred": 1
}
```

## Edge case

Input:
```json
{
  "values": [],
  "key": "status"
}
```
Output:
```json
{}
```

## Limits

- Records without the field are counted under "undefined".
- A null-prototype accumulator keeps special keys as data.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/count-by-the-properties-of-an-array-of-objects.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses a null-prototype accumulator. Original MDX retained as inert source evidence.
