---
title: reduce – รวมหรือสะสมอาร์เรย์เป็นค่าเดียว (aggregate)
url: https://www.thepexcel.com/functions/n8n/array-functions/reduce-n8n/
type: function-explainer
program: n8n
syntax: "array.reduce((accumulator, currentValue) => newAccumulator, initialValue)"
date: 2025-12-16
updated: 2025-12-17
scores:
  popularity: 6
  difficulty: 6
  usefulness: 7
---

# reduce – รวมหรือสะสมอาร์เรย์เป็นค่าเดียว (aggregate)

> รวมหรือสะสมอาร์เรย์เป็นค่าเดียว (aggregate to single value)

## คำอธิบาย

reduce() เป็น JavaScript standard array method (ES2021+, ไม่ใช่ n8n-specific) ประมวลผลแต่ละรายการในอาร์เรย์โดยใช้ reducer function (accumulator, currentValue) => newAccumulator คืนค่าผลลัพธ์เดียวที่ได้จากการรวมหรือสะสมค่า มีประโยชน์ในการหาผลรวม ผลคูณ นับจำนวน สร้าง objects/maps หรือคำนวณค่า statistics

## Syntax

```excel
array.reduce((accumulator, currentValue) => newAccumulator, initialValue)
```

**Variant**

```excel
$json.items.reduce((sum, item) => sum + item.price, 0)
```

หาผลรวมของ price จากทุก items

**Variant**

```excel
$json.users.reduce((map, user) => ({ ...map, [user.id]: user }), {})
```

สร้าง object map โดยใช้ user.id เป็น key

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| reducer | Yes | function |  | ฟังก์ชัน reducer (accumulator, currentValue) => newAccumulator ที่รับค่าสะสมและรายการปัจจุบัน แล้วคืนค่าสะสมใหม่ |
| initialValue | No | any |  | ค่าเริ่มต้นของ accumulator (แนะนำให้ระบุเสมอ) ถ้าไม่ระบุจะใช้รายการแรกในอาร์เรย์ |

## เคสการใช้งาน

### หาผลรวม/ผลคูณ

รวมราคา ยอดขาย หรือคำนวณค่า statistics จากอาร์เรย์

_เหมาะกับ:_ sum-calculation

### นับจำนวนตามเงื่อนไข

นับรายการที่ตรงเงื่อนไข เช่น สินค้าที่มีในสต็อก ผู้ใช้ที่ active

_เหมาะกับ:_ conditional-counting

### สร้าง object/map

แปลง array เป็น object/map โดยใช้ key เฉพาะ เช่น user ID หรือ product code

_เหมาะกับ:_ object-creation

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: หาผลรวมตัวเลข

```excel
{{ [1, 2, 3, 4, 5].reduce((sum, n) => sum + n, 0) }}
```

**ผลลัพธ์:** `15`

รวมตัวเลข: เริ่มจาก 0 + 1 → 1 + 2 → 3 + 3 → 6 + 4 → 10 + 5 = 15

### 2. ตัวอย่างที่ 2: รวมราคาสินค้า

```excel
{{ [{ name: 'A', price: 100 }, { name: 'B', price: 200 }, { name: 'C', price: 150 }].reduce((total, item) => total + item.price, 0) }}
```

**ผลลัพธ์:** `450`

รวมราคาสินค้าทั้งหมด: 0 + 100 + 200 + 150 = 450

### 3. ตัวอย่างที่ 3: นับจำนวนตามเงื่อนไข

```excel
{{ [{ name: 'A', inStock: true }, { name: 'B', inStock: false }, { name: 'C', inStock: true }].reduce((count, item) => item.inStock ? count + 1 : count, 0) }}
```

**ผลลัพธ์:** `2`

นับสินค้าที่ inStock === true ได้ 2 รายการ (A และ C)

### 4. ตัวอย่างที่ 4: สร้าง object map โดยใช้ ID เป็น key

```excel
{{ [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }].reduce((map, user) => ({ ...map, [user.id]: user.name }), {}) }}
```

**ผลลัพธ์:** `{ '1': 'John', '2': 'Jane' }`

แปลง array เป็น object map ที่ใช้ user ID เป็น key และ name เป็น value

## หมายเหตุเพิ่มเติม

⚠️ reduce() เป็น JavaScript standard method (ES2021+) ไม่ใช่ n8n-specific extension รับ 2 parameters: reducer function และ initialValue (แนะนำให้ระบุเสมอ) reducer function รับ (accumulator, currentValue, index, array) แต่ส่วนใหญ่ใช้แค่ 2 ตัวแรก powerful แต่อาจอ่านยาก ใช้ map()/filter() ก่อนถ้าเหมาะสม

## คำถามที่พบบ่อย

**Q: reduce() ต้องระบุ initialValue หรือไม่?**

ไม่บังคับ แต่แนะนำให้ระบุเสมอเพื่อหลีกเลี่ยง errors ถ้าไม่ระบุจะใช้รายการแรกเป็น initialValue และเริ่มลูปจากรายการที่ 2 ซึ่งอาจทำให้เกิดปัญหาถ้า array ว่าง

**Q: reduce() กับ map() + sum() ต่างกันอย่างไร?**

reduce() ยืดหยุ่นกว่า ทำได้หลายอย่าง (sum, count, object creation) ใน 1 ลูป ส่วน map() + sum() ต้องลูป 2 รอบ แต่ map() + sum() อ่านง่ายกว่าสำหรับ simple cases ใช้ reduce() เมื่อต้องการ complex aggregation

**Q: reduce() จะคืนค่า type อะไร?**

คืนค่า type เดียวกับ initialValue เช่น initialValue เป็น 0 → คืนค่า number, initialValue เป็น {} → คืนค่า object, initialValue เป็น [] → คืนค่า array

**Q: ใช้ reduce() เมื่อไหร่?**

ใช้เมื่อต้องการ aggregate อาร์เรย์เป็นค่าเดียว เช่น หาผลรวม ผลคูณ average นับจำนวน สร้าง object/map flatten nested arrays หรือคำนวณ statistics ซับซ้อน ใช้คู่กับ filter() เพื่อกรองก่อน reduce() ใช้คู่กับ map() เพื่อ transform ก่อน reduce()

## ฟังก์ชันที่เกี่ยวข้อง

- [map – แปลงแต่ละรายการในอาร์เรย์ (transform elements)](https://www.thepexcel.com/functions/n8n/array-functions/map-n8n/)
- [filter – กรองรายการจากอาร์เรย์ตามเงื่อนไข](https://www.thepexcel.com/functions/n8n/array-functions/filter-n8n/)
- find-n8n
- [sum() – รวมค่าตัวเลขในอาเรย์](https://www.thepexcel.com/functions/n8n/array-functions/sum-n8n/)

---

_Source: [https://www.thepexcel.com/functions/n8n/array-functions/reduce-n8n/](https://www.thepexcel.com/functions/n8n/array-functions/reduce-n8n/)_
