Hướng dẫn how do you write a reduce method in javascript? - làm thế nào để bạn viết một phương pháp rút gọn trong javascript?

Phương thức

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 thực thi chức năng gọi lại "giảm" do người dùng cung cấp trên mỗi phần tử của mảng, theo thứ tự, chuyển giá trị trả về từ tính toán trên phần tử trước. Kết quả cuối cùng của việc chạy bộ giảm tốc trên tất cả các phần tử của mảng là một giá trị duy nhất.
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5
method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

Lần đầu tiên gọi lại, không có "giá trị trả về của tính toán trước". Nếu được cung cấp, một giá trị ban đầu có thể được sử dụng ở vị trí của nó. Mặt khác, phần tử mảng tại chỉ mục 0 được sử dụng làm giá trị ban đầu và lần lặp bắt đầu từ phần tử tiếp theo [chỉ mục 1 thay vì chỉ mục 0].

Có lẽ trường hợp dễ hiểu nhất đối với

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 là trả lại tổng của tất cả các yếu tố trong một mảng:

Thử nó

Bộ giảm tốc đi qua phần tử phần tử, ở mỗi bước, thêm giá trị mảng hiện tại vào kết quả từ bước trước [kết quả này là tổng chạy của tất cả các bước trước đó]-cho đến khi không còn phần tử nào nữa.

Cú pháp

// Arrow function
reduce[[accumulator, currentValue] => { /* … */ }]
reduce[[accumulator, currentValue, currentIndex] => { /* … */ }]
reduce[[accumulator, currentValue, currentIndex, array] => { /* … */ }]

reduce[[accumulator, currentValue] => { /* … */ }, initialValue]
reduce[[accumulator, currentValue, currentIndex] => { /* … */ }, initialValue]
reduce[[accumulator, currentValue, currentIndex, array] => { /* … */ }, initialValue]

// Callback function
reduce[callbackFn]
reduce[callbackFn, initialValue]

// Inline callback function
reduce[function [accumulator, currentValue] { /* … */ }]
reduce[function [accumulator, currentValue, currentIndex] { /* … */ }]
reduce[function [accumulator, currentValue, currentIndex, array] { /* … */ }]

reduce[function [accumulator, currentValue] { /* … */ }, initialValue]
reduce[function [accumulator, currentValue, currentIndex] { /* … */ }, initialValue]
reduce[function [accumulator, currentValue, currentIndex, array] { /* … */ }, initialValue]

Thông số

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7

Một hàm để thực thi cho mỗi phần tử trong mảng. Giá trị trả về của nó trở thành giá trị của tham số

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8 trong lần gọi tiếp theo của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7. Đối với lệnh gọi cuối cùng, giá trị trả về trở thành giá trị trả về của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5.

Hàm được gọi với các đối số sau:

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8

Giá trị do cuộc gọi trước đó đến

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7. Trong cuộc gọi đầu tiên,
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 nếu được chỉ định, nếu không thì giá trị của
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
4.

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5

Giá trị của phần tử hiện tại. Trong cuộc gọi đầu tiên, giá trị của

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
4 nếu
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 được chỉ định, nếu không thì giá trị của
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
8.

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
9

Vị trí chỉ số của

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5 trong mảng. Trong cuộc gọi đầu tiên,
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
1 nếu
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 được chỉ định, nếu không
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
3.

[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
4

Mảng

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 đã được yêu cầu.

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 Tùy chọnOptional

Một giá trị mà

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8 được khởi tạo ngay lần đầu tiên gọi lại. Nếu
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 được chỉ định, điều đó cũng khiến
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5 được khởi tạo thành giá trị đầu tiên trong mảng. Nếu
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 không được chỉ định,
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8 được khởi tạo thành giá trị thứ nhất trong mảng và
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5 được khởi tạo thành giá trị thứ hai trong mảng.

Giá trị trả về

Giá trị kết quả từ việc chạy chức năng gọi lại "giảm" để hoàn thành toàn bộ mảng.

Ngoại lệ

const objects = [{ x: 1 }, { x: 2 }, { x: 3 }];
const sum = objects.reduce[
  [accumulator, currentValue] => accumulator + currentValue.x,
  0,
];

console.log[sum]; // 6
3

Mảng không chứa các phần tử và

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 không được cung cấp.

Sự mô tả

Phương pháp

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 là một phương pháp lặp. Nó chạy chức năng gọi lại "bộ giảm thiểu" trên tất cả các phần tử trong mảng, theo thứ tự chỉ số tăng dần và tích lũy chúng thành một giá trị duy nhất. Mỗi lần, giá trị trả lại của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 được chuyển thành
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 một lần nữa trong lần gọi tiếp theo là
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8. Giá trị cuối cùng của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8 [là giá trị được trả về từ
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 trên lần lặp cuối cùng của mảng] trở thành giá trị trả về của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5.

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 chỉ được gọi cho các chỉ mục mảng đã gán các giá trị. Nó không được gọi cho các khe trống trong các mảng thưa thớt.

Không giống như các phương thức lặp khác,

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 không chấp nhận đối số
const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]
4.
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 luôn được gọi với
const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]
6 là
const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]
7, được thay thế bằng
const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]
8 nếu
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 không nghiêm ngặt.

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 là một khái niệm trung tâm trong lập trình chức năng, trong đó không thể thay đổi bất kỳ giá trị nào, vì vậy để tích lũy tất cả các giá trị trong một mảng, người ta phải trả về giá trị tích lũy mới trên mỗi lần lặp. Công ước này tuyên truyền đến
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 của JavaScript: Bạn nên sử dụng các phương thức sao chép hoặc sao chép khác khi có thể tạo các mảng và đối tượng mới làm bộ tích lũy, thay vì biến đổi phương thức hiện có. Nếu bạn quyết định đột biến bộ tích lũy thay vì sao chép nó, hãy nhớ vẫn trả về đối tượng đã sửa đổi trong cuộc gọi lại hoặc lần lặp tiếp theo sẽ nhận được không xác định.

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 không làm biến đổi mảng mà nó được gọi, nhưng chức năng được cung cấp như
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7 có thể. Tuy nhiên, lưu ý rằng độ dài của mảng được lưu trước khi gọi đầu tiên của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7. Vì vậy:

  • const getMax = [a, b] => Math.max[a, b];
    
    // callback is invoked for each element in the array starting at index 0
    [1, 100].reduce[getMax, 50]; // 100
    [50].reduce[getMax, 10]; // 50
    
    // callback is invoked once for element at index 1
    [1, 100].reduce[getMax]; // 100
    
    // callback is not invoked
    [50].reduce[getMax]; // 50
    [].reduce[getMax, 1]; // 1
    
    [].reduce[getMax]; // TypeError
    
    7 sẽ không truy cập bất kỳ yếu tố nào được thêm vào ngoài độ dài ban đầu của mảng khi cuộc gọi đến
    const getMax = [a, b] => Math.max[a, b];
    
    // callback is invoked for each element in the array starting at index 0
    [1, 100].reduce[getMax, 50]; // 100
    [50].reduce[getMax, 10]; // 50
    
    // callback is invoked once for element at index 1
    [1, 100].reduce[getMax]; // 100
    
    // callback is not invoked
    [50].reduce[getMax]; // 50
    [].reduce[getMax, 1]; // 1
    
    [].reduce[getMax]; // TypeError
    
    5 bắt đầu.
  • Các thay đổi đối với các chỉ mục đã được truy cập không khiến
    const getMax = [a, b] => Math.max[a, b];
    
    // callback is invoked for each element in the array starting at index 0
    [1, 100].reduce[getMax, 50]; // 100
    [50].reduce[getMax, 10]; // 50
    
    // callback is invoked once for element at index 1
    [1, 100].reduce[getMax]; // 100
    
    // callback is not invoked
    [50].reduce[getMax]; // 50
    [].reduce[getMax, 1]; // 1
    
    [].reduce[getMax]; // TypeError
    
    7 được gọi lại trên chúng.
  • Nếu một phần tử hiện có, chưa được liên kết của mảng được thay đổi bởi
    const getMax = [a, b] => Math.max[a, b];
    
    // callback is invoked for each element in the array starting at index 0
    [1, 100].reduce[getMax, 50]; // 100
    [50].reduce[getMax, 10]; // 50
    
    // callback is invoked once for element at index 1
    [1, 100].reduce[getMax]; // 100
    
    // callback is not invoked
    [50].reduce[getMax]; // 50
    [].reduce[getMax, 1]; // 1
    
    [].reduce[getMax]; // TypeError
    
    7, giá trị của nó được chuyển cho
    const getMax = [a, b] => Math.max[a, b];
    
    // callback is invoked for each element in the array starting at index 0
    [1, 100].reduce[getMax, 50]; // 100
    [50].reduce[getMax, 10]; // 50
    
    // callback is invoked once for element at index 1
    [1, 100].reduce[getMax]; // 100
    
    // callback is not invoked
    [50].reduce[getMax]; // 50
    [].reduce[getMax, 1]; // 1
    
    [].reduce[getMax]; // TypeError
    
    7 sẽ là giá trị tại thời điểm phần tử đó được truy cập. Các yếu tố bị xóa không được truy cập.

CẢNH BÁO: Sửa đổi đồng thời của loại được mô tả ở trên thường xuyên dẫn đến mã khó hiểu và thường được tránh [ngoại trừ trong các trường hợp đặc biệt]. Concurrent modifications of the kind described above frequently lead to hard-to-understand code and are generally to be avoided [except in special cases].

Phương pháp

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 là chung chung. Nó chỉ mong đợi giá trị
const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]
7 sẽ có thuộc tính
const people = [
  { name: "Alice", age: 21 },
  { name: "Max", age: 20 },
  { name: "Jane", age: 20 },
];

function groupBy[objectArray, property] {
  return objectArray.reduce[[acc, obj] => {
    const key = obj[property];
    const curGroup = acc[key] ?? [];

    return { ...acc, [key]: [...curGroup, obj] };
  }, {}];
}

const groupedPeople = groupBy[people, "age"];
console.log[groupedPeople];
// {
//   20: [
//     { name: 'Max', age: 20 },
//     { name: 'Jane', age: 20 }
//   ],
//   21: [{ name: 'Alice', age: 21 }]
// }
2 và các thuộc tính được khóa.

Khi nào không sử dụng giảm []

Các chức năng đệ quy như

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 có thể mạnh mẽ nhưng đôi khi khó hiểu, đặc biệt là đối với các nhà phát triển JavaScript ít có kinh nghiệm. Nếu mã trở nên rõ ràng hơn khi sử dụng các phương thức mảng khác, các nhà phát triển phải cân nhắc sự đánh đổi khả năng đọc đối với các lợi ích khác của việc sử dụng
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5. Trong trường hợp
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 là sự lựa chọn tốt nhất, tài liệu và đặt tên biến ngữ nghĩa có thể giúp giảm thiểu những hạn chế dễ đọc.

Trường hợp cạnh

Nếu mảng chỉ có một phần tử [bất kể vị trí] và không được cung cấp

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 hoặc nếu
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 được cung cấp nhưng mảng trống, giá trị solo sẽ được trả về mà không gọi
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
7.

Nếu

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 được cung cấp và mảng không trống, thì phương thức giảm sẽ luôn gọi chức năng gọi lại bắt đầu từ INDEX 0.

Nếu

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 không được cung cấp thì phương pháp giảm sẽ hoạt động khác nhau cho các mảng có chiều dài lớn hơn 1, bằng 1 và 0, như được hiển thị trong ví dụ sau:

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError

Ví dụ

Làm thế nào giảm [] hoạt động mà không có giá trị ban đầu

Mã dưới đây cho thấy những gì xảy ra nếu chúng ta gọi

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 với một mảng và không có giá trị ban đầu.

const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];

Cuộc gọi lại sẽ được gọi bốn lần, với các đối số và các giá trị trả về trong mỗi cuộc gọi như sau:

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
4Giá trị trả về
Cuộc gọi đầu tiên
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
5
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
6
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
3
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
8
Cuộc gọi thứ hai
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
8
Cuộc gọi thứ hai
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
0
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
1
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
2
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
1
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
2
Cuộc gọi thứ ba
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
4
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
5
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
4
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
5
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
6
Cuộc gọi thứ tư

const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
8

const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
9

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
00

[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];

Tham số

[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
4 không bao giờ thay đổi thông qua quy trình - nó luôn luôn là
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
02. Giá trị được trả về bởi
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 sẽ là của lệnh gọi gọi lại cuối cùng [
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
00].

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
8
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
5
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
4Giá trị trả về
Cuộc gọi đầu tiên
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
5
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
5
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
6
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
3
Cuộc gọi thứ hai
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
3
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
6
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
3
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
8
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
2
// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]
8
Cuộc gọi thứ hai
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
0
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
1
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
5
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
1
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
2
Cuộc gọi thứ ba
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
4
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
5
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
4
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
5
const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
6
Cuộc gọi thứ tư

const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
8

const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];
9

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
00must supply an
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3, so that each item passes through your function.

const objects = [{ x: 1 }, { x: 2 }, { x: 3 }];
const sum = objects.reduce[
  [accumulator, currentValue] => accumulator + currentValue.x,
  0,
];

console.log[sum]; // 6

Tham số
[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
4 không bao giờ thay đổi thông qua quy trình - nó luôn luôn là
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
02. Giá trị được trả về bởi
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 sẽ là của lệnh gọi gọi lại cuối cùng [
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
00].

const flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce[[accumulator, currentValue] => accumulator.concat[currentValue], []];
// flattened is [0, 1, 2, 3, 4, 5]

Làm thế nào giảm [] hoạt động với giá trị ban đầu

const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];

const countedNames = names.reduce[[allNames, name] => {
  const currCount = allNames[name] ?? 0;
  return {
    ...allNames,
    [name]: currCount + 1,
  };
}, {}];
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }

Ở đây chúng tôi giảm cùng một mảng bằng cách sử dụng cùng một thuật toán, nhưng với
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3 của
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
06 được chuyển làm đối số thứ hai cho
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5:

const people = [
  { name: "Alice", age: 21 },
  { name: "Max", age: 20 },
  { name: "Jane", age: 20 },
];

function groupBy[objectArray, property] {
  return objectArray.reduce[[acc, obj] => {
    const key = obj[property];
    const curGroup = acc[key] ?? [];

    return { ...acc, [key]: [...curGroup, obj] };
  }, {}];
}

const groupedPeople = groupBy[people, "age"];
console.log[groupedPeople];
// {
//   20: [
//     { name: 'Max', age: 20 },
//     { name: 'Jane', age: 20 }
//   ],
//   21: [{ name: 'Alice', age: 21 }]
// }

Cuộc gọi lại sẽ được gọi năm lần, với các đối số và các giá trị trả về trong mỗi cuộc gọi như sau:

// friends - an array of objects
// where object field "books" is a list of favorite books
const friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
const allbooks = friends.reduce[
  [accumulator, currentValue] => [...accumulator, ...currentValue.books],
  ["Alphabet"],
];
console.log[allbooks];
// [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
06

[15, 16, 17, 18, 19].reduce[
  [accumulator, currentValue] => accumulator + currentValue,
  10,
];
1 The same effect can be achieved with
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
34 and
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
35 as
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
36 with better performance.

const myArray = ["a", "b", "a", "b", "c", "e", "e", "c", "d", "d", "d", "d"];
const myArrayWithNoDuplicates = myArray.reduce[[accumulator, currentValue] => {
  if [!accumulator.includes[currentValue]] {
    return [...accumulator, currentValue];
  }
  return accumulator;
}, []];

console.log[myArrayWithNoDuplicates];

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
14

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
18

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
0

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
22

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
1

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
26

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
2

Cuộc gọi thứ năm

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
30

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
3

Giá trị được trả về bởi
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
5 trong trường hợp này sẽ là
const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
30.

Tổng các giá trị trong một mảng đối tượng

const getMax = [a, b] => Math.max[a, b];

// callback is invoked for each element in the array starting at index 0
[1, 100].reduce[getMax, 50]; // 100
[50].reduce[getMax, 10]; // 50

// callback is invoked once for element at index 1
[1, 100].reduce[getMax]; // 100

// callback is not invoked
[50].reduce[getMax]; // 50
[].reduce[getMax, 1]; // 1

[].reduce[getMax]; // TypeError
4

Để tổng hợp các giá trị có trong một mảng các đối tượng, bạn phải cung cấp một
const array = [15, 16, 17, 18, 19];

function reducer[accumulator, currentValue, index] {
  const returns = accumulator + currentValue;
  console.log[
    `accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}`,
  ];
  return returns;
}

array.reduce[reducer];
3, để mỗi mục đi qua chức năng của bạn.

Làm phẳng một mảng mảng
Đếm các phiên bản của các giá trị trong một đối tượng
# sec-array.prototype.reduce

Nhóm đối tượng bởi một tài sản

Các mảng kết nối có trong một mảng các đối tượng bằng cách sử dụng cú pháp lan truyền và initValue

Xóa các mục trùng lặp trong một mảng

Làm thế nào thực hiện giảm trong JavaScript?

Bất cứ khi nào bạn cảm thấy bạn đã sẵn sàng, hãy thực hiện các bước tiếp theo về cách thực hiện tùy chỉnh của bạn []: Khởi tạo biến tích lũy với đối số 0 hoặc initalValue từ giảm []. Vòng lặp qua các phần tử mảng. Gọi hàm giảm thiểu với bộ tích lũy và phần tử hiện tại là đối số.

Giảm cái gì trong JavaScript với ví dụ?

Phương thức giảm [] trong JavaScript được sử dụng để giảm mảng xuống 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 một bộ tích lũy.Cú pháp: Array.Reduce [Hàm [Total, CurrentValue, CurrentIndex, ARR], initValue]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 ]

Phương pháp giảm là gì?

Phương pháp giảm chênh lệch về cơ bản bao gồm mã hóa nhị phân của số bước để đi từ chỉ số của giá trị quan trọng cuối cùng đến chỉ số của giá trị quan trọng hiện tại.Từ: Những tiến bộ trong hình ảnh và vật lý điện tử, 2002.a binary encoding of the number of steps to go from the index of the last significant value to the index of the present significant value. From: Advances in Imaging and Electron Physics, 2002.

Bạn có thể sử dụng giảm trên một javascript đối tượng không?

Nhưng bạn có biết bạn cũng có thể giảm chúng thành các đối tượng không?Đầu tiên, một cái nhìn tổng quan nhanh về cách giảm hoạt động.Phương pháp giảm nhận được hai đối số: một hàm và giá trị ban đầu.Hàm sẽ chạy cho mọi giá trị trong mảng và cũng nhận được hai đối số: bộ tích lũy hoặc ACC và giá trị hiện tại.you can reduce them to objects as well? First, a quick overview of how reduce works. The reduce method receives two arguments: a function and an initial value. The function will run run for every value in the array, and receives two arguments as well: the accumulator, or acc , and the current value .

Bài Viết Liên Quan

Chủ Đề