---
title: average – ฟังก์ชันคำนวณค่าเฉลี่ยของ Array
url: https://www.thepexcel.com/functions/n8n/array-functions/average-n8n/
type: function-explainer
program: n8n
syntax: array.average()
date: 2025-12-16
updated: 2025-12-24
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# average – ฟังก์ชันคำนวณค่าเฉลี่ยของ Array

> average คำนวณค่าเฉลี่ยเลขคณิต (arithmetic mean) ของตัวเลขทั้งหมดในอาร์เรย์ ใช้เมื่อต้องการหาค่าเฉลี่

## คำอธิบาย

average คำนวณค่าเฉลี่ยเลขคณิต (arithmetic mean) ของตัวเลขทั้งหมดในอาร์เรย์ ใช้เมื่อต้องการหาค่าเฉลี่ยยอดขาย คะแนนเฉลี่ย อุณหภูมิเฉลี่ย หรือเมตริกอื่นๆ ในการประมวลผล workflow

## Syntax

```excel
array.average()
```

**Variant**

```excel
$json.numbers.average()
```

ใช้เป็น method เมื่อ $json.numbers เป็น array ของตัวเลข

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| array | Yes | number[] |  | Array ที่มีค่าตัวเลข (เช่น [10, 20, 30, 40]) ฟังก์ชันจะรวมค่าทั้งหมดแล้วหารด้วยจำนวนรายการ |

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

### คำนวณค่าเฉลี่ยยอดขาย

ใช้ฟังก์ชันนี้เพื่อหาค่าเฉลี่ยยอดขายรายวันหรือรายสัปดาห์จากข้อมูลสั่งซื้อ

_เหมาะกับ:_ data-analysis

### วิเคราะห์ประสิทธิภาพ

หาค่าเฉลี่ยของเวลาในการประมวลผล หรือจำนวนข้อมูลที่ผ่านการตรวจสอบต่อรอบ

_เหมาะกับ:_ performance-monitoring

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน: คำนวณค่าเฉลี่ยจากตัวเลข

```excel
{{ [10, 20, 30, 40, 50].average() }}
```

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

ฟังก์ชันจะรวม 10+20+30+40+50 = 150 แล้วหารด้วย 5 ได้ 30 เป็นค่าเฉลี่ย

### 2. ตัวอย่าง Workflow: หาค่าเฉลี่ยยอดขายรายวัน

```excel
{{ $json.dailySales.map(x => x.total).average() }}
```

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

ดึงค่า total จากทุกรายการใน dailySales อาร์เรย์ แล้วคำนวณค่าเฉลี่ย ใช้เมื่อต้องการรายงานยอดขายเฉลี่ยต่อวัน

### 3. ตัวอย่าง: ค่าเฉลี่ยของคะแนนผู้ใช้

```excel
{{ $json.users.filter(u => u.active).map(u => u.score).average() }}
```

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

กรองเฉพาะผู้ใช้ที่ active จากนั้นดึงคะแนนของพวกเขา คำนวณค่าเฉลี่ย ประโยชน์ในการวัดคะแนนของผู้ใช้ที่ทำงานอยู่เท่านั้น

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

- ทำความสะอาดข้อมูลก่อน: ใช้ filter() เพื่อตัดค่า null/undefined ออก ตัวอย่าง {{ $json.data.filter(x => typeof x === 'number').average() }}

- เก็บค่าไว้ในตัวแปร: {{ let scores = $json.users.map(u => u.score); scores.average() }} ทำให้โค้ดอ่านง่าย

- ใช้กับ Set Node: สามารถใช้ average() ใน Set Node (Expression mode) เพื่อตั้งค่าฟิลด์โดยตรง

- รวมกับ Round: ใช้ Math.round() เพื่อปัดเศษ {{ $json.prices.average().round() }} หรือ {{ Math.round($json.prices.average() * 100) / 100 }}

- ตรวจสอบความยาว array ก่อน: ประเมินค่า average ด้วย if condition เพื่อหลีกเลี่ยง division by zero

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

**Q: ถ้า array มี null หรือ undefined ฟังก์ชัน average() จะทำอย่างไร?**

average() โดยทั่วไปจะข้ามค่า null/undefined และคำนวณเฉพาะตัวเลขที่ถูกต้อง แต่ลองทดสอบกับข้อมูลจริงของคุณ ถ้ามีปัญหา ใช้ filter((x => x != null) ก่อนเพื่อทำความสะอาดข้อมูล

**Q: ความแตกต่างระหว่าง average() และ sum() คืออะไร?**

average() = (sum of values) / (count of items) ส่วน sum() เพียงแค่รวมค่าทั้งหมด average() ดีกว่าเมื่อต้องเปรียบเทียบข้อมูลที่มีจำนวนรายการต่างกัน

**Q: ถ้า array ว่างเปล่า ([]) ฟังก์ชันจะคืนค่าอะไร?**

array ว่างเปล่าอาจคืนค่า 0 หรือ undefined ขึ้นอยู่กับเวอร์ชัน n8n ควรใช้ if statement เช่น {{ $json.values.length > 0 ? $json.values.average() : 0 }} เพื่อป้องกัน error

**Q: ใช้ average() ในไหนของ workflow?**

ใช้ได้ใน Code Node (เขียนโค้ด JavaScript) หรือ Set Node (Expression mode) ส่วน IF Node ใช้เพื่อตรวจสอบค่าเฉลี่ยว่าสูงหรือต่ำกว่า threshold

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

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

## แหล่งข้อมูลเพิ่มเติม

- [n8n Documentation: Expressions](https://docs.n8n.io/code/expressions/) _(article)_
- [n8n Built-in Functions Overview](https://docs.n8n.io/code/builtin/overview/) _(article)_
- [Array Functions in n8n Code](https://docs.n8n.io/code/builtin/data-transformation-functions/) _(article)_

---

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