Phương thức reduce() duyệt mảng từ đầu đến cuối, gộp các phần tử thành giá trị đơn qua hàm callback.
Tham số:
- callback – (accumulator, currentValue, index, array) => newAccumulator.
- initialValue (tùy chọn) – Giá trị khởi tạo accumulator.
Ví dụ
<html>
<head>
<title>JavaScript Array reduce Method</title>
</head>
<body>
<script>
var total = [0, 1, 2, 3].reduce(function(a, b) { return a + b; });
document.write("total is: " + total); // 6
</script>
</body>
</html>