Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcArray reduce() method.
- Example:
<!DOCTYPE html>
Bàn luận
<
head
>
<
<!DOCTYPE html>
0>
<!DOCTYPE html>
2<!DOCTYPE html>
3
<!DOCTYPE html>
5<!DOCTYPE html>
0>
<!DOCTYPE html>
5head
>
Dưới đây là ví dụ về phương thức giảm mảng ().
<
html
>
<
html
8>
<!DOCTYPE html>
2>
1
<!DOCTYPE html>
2>
3
<!DOCTYPE html>
5html
8>
<
<
2 <
3<
4<
5>
______2929
<!DOCTYPE html>
5<
0>
<
head
3head
4head
3>
<
<
0 <
1<
4<
3>
<
>
8>
<!DOCTYPE html>
2
1
<!DOCTYPE html>
2
3
4
5
<!DOCTYPE html>
2
7
<!DOCTYPE html>
2
9
4<
1
<
2<
3
<!DOCTYPE html>
2
7
<!DOCTYPE html>
5>
8>
<!DOCTYPE html>
5<
2>
<!DOCTYPE html>
2<
6
- Output:
head
8head
9 >
0<
4>
2>
3head
9>
arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator.
Syntax:
array.reduce( function(total, currentValue, currentIndex, arr),
initialValue )
<!DOCTYPE html>
5html
<!DOCTYPE html>
05 This method accepts
five parameters as mentioned above and described below:
- Phương thức ARR.Reduce () trong JavaScript được sử dụng để giảm mảng thành một giá trị duy nhất và thực thi hàm được cung cấp cho từng giá trị của mảng (từ từ trái sang phải) và giá trị trả về của hàm được lưu trữ trong bộ tích lũy . It is the required parameter and used to run for each element of array. It contains four parameter which are listed below:
- Tham số: Phương pháp này chấp nhận năm tham số như đã đề cập ở trên và được mô tả dưới đây: It is required parameter and used to specify the initialValue, or the previously returned value of the function.
- Hàm (tổng số, currentValue, index, mảng): Đây là tham số cần thiết và được sử dụng để chạy cho từng phần tử của mảng. Nó chứa bốn tham số được liệt kê dưới đây: It is required parameter and used to specify the value of the
current element.
- Tổng số: Đó là tham số yêu cầu và được sử dụng để chỉ định initValue hoặc giá trị được trả về trước đó của hàm. It is optional parameter and used to specify the array index of the current element.
- currentValue: nó được yêu cầu tham số và được sử dụng để chỉ định giá trị của phần tử hiện tại. It is optional parameter and used to specify the array object the current element belongs to.
- current Index: Đây là tham số tùy chọn và được sử dụng để chỉ định chỉ số mảng của phần tử hiện tại. It is optional parameter and used to specify the value to be passed to the function as the initial value.
ARR: Đó là tham số tùy chọn và được sử dụng để chỉ định đối tượng mảng mà phần tử hiện tại thuộc về. This example use reduce() method to
return the sum of all array elements.
<!DOCTYPE html>
<
html
>
<
head
>
<
<!DOCTYPE html>
0>
<!DOCTYPE html>
2<!DOCTYPE html>
3
<!DOCTYPE html>
5<!DOCTYPE html>
0>
<!DOCTYPE html>
5head
>
Dưới đây là ví dụ về phương thức giảm mảng ().
<
html
>
<
html
8>
<!DOCTYPE html>
2<!DOCTYPE html>
46
<!DOCTYPE html>
2<!DOCTYPE html>
48
<!DOCTYPE html>
5html
8>
<
<
2 <
3<
4<
5>
<!DOCTYPE html>
2<
6
<!DOCTYPE html>
5<
0>
<
head
3head
4head
3>
______2929
<
>
8>
<!DOCTYPE html>
2<!DOCTYPE html>
86
<!DOCTYPE html>
2<!DOCTYPE html>
88
4<!DOCTYPE html>
90
<!DOCTYPE html>
2
7
<!DOCTYPE html>
2
9
4<
1
<
2<!DOCTYPE html>
98
<!DOCTYPE html>
2
7
<!DOCTYPE html>
5>
8>
<!DOCTYPE html>
5<
2>
<!DOCTYPE html>
5html
<
10
Output:
<
<
0 <
1<
4<
3>
This example use reduce() method to return the round sum of all array elements.
<!DOCTYPE html>
<
html
>
<
head
>
<
<!DOCTYPE html>
0>
<!DOCTYPE html>
2<!DOCTYPE html>
3
<!DOCTYPE html>
5<!DOCTYPE html>
0>
<!DOCTYPE html>
5head
>
Dưới đây là ví dụ về phương thức giảm mảng ().
<
html
>
<
<
2 <
3<
4<
5>
<!DOCTYPE html>
2<!DOCTYPE html>
46
<!DOCTYPE html>
2<!DOCTYPE html>
48
<!DOCTYPE html>
5html
8>
<
<
2 <
3<
4<
5>
<!DOCTYPE html>
2<
6
<!DOCTYPE html>
5<
0>
<
head
3head
4head
3>
______2929
<
>
8>
<!DOCTYPE html>
2<
91
<!DOCTYPE html>
2<!DOCTYPE html>
88
4<
95
<!DOCTYPE html>
2
7
<
<
0 <
1<
4<
3>
4<
1
<
2html
03
<!DOCTYPE html>
2
7
<!DOCTYPE html>
5>
8>
<!DOCTYPE html>
5<
2>
<!DOCTYPE html>
5html
<
10
<!DOCTYPE html>
2<
6
Supported Browsers: The browsers supported by JavaScript Array reduce() method are listed below:- Google Chrome 3 trở lên
- Microsoft Edge 12 trở lên
- Mozilla Firefox 3.0 trở lên
- Safari 5 trở lên
- Opera 10.5 trở lên