JavaScript Arrays : To taub cov txo muaj nuj nqi

syntax

arr.reduce(callbackFunction, [initialValue])

Li cas kom tsis txhob ua?

Txo yog ib tug muaj nuj nqi uas loops los ntawm ib tug array.

Operations yuav ua tau rau lub array ntawm ib tug callback muaj nuj nqi.

Qhov kawg ntawm cov neeg ua haujlwm nyob rau hauv lub array, txo rov qab ib zaug xwb tus nqi

Reduce can be used, for example, to total a shopping cart

Piv txwv li

  cia arr = [1,2,3,4,5];
  arr.reduce( (accumulator, currentValue, currentIndex, array) => { 
      //ib co ua hauj lwm;
      rov qab ib yam dab tsi;
  });

 

Lub callback muaj nuj nqi

Lub callback muaj nuj nqi yog invoked rau txhua lub caij nyob rau hauv lub array, dua nyob rau hauv 4 arguments which are passed in by the reduce function.

These 4 cov lus sib cav ces yuav tau siv nyob rau hauv peb callback muaj nuj nqi.

Yuav siv sij hawm plaub nqe lus

  1. accumulator – Tus thawj lub caij ntawm lub array rau tus thawj lub voj los yog thawj zaug nqi yog tias nkag. Ces tus xa rov qab nqi ntawm yav tas los lub voj.
  2. currentValue – Tus tam sim no lub caij nyob rau hauv lub array. Pib los ntawm tus thib ob lub caij nyob rau hauv lub array ie. arr[1], thaum thawj zaug nqi sib cav yog tsis tau dhau. Yog hais tias cov thawj zaug nqi sib cav yog dhau ces tam sim no tus nqi yog pib los ntawm 1 lub caij nyob rau hauv lub array arr[0].
  3. currentIndex – Qhov Performance index ntawm qhov tam sim no lub caij.
  4. array – Tag nrho cov array.

ua hauj lwm Piv txwv li

cia arr = [1,2,3,4,5];

arr.reduce(
  (accumulator, currentValue, currentIndex, array) => {
    console.log('accumulator (Nyob rau 1 lub voj, accumulator yog arr[0], ces nws yog ib lub rov qab tus nqi ntawm yav tas los lub voj): ${accumulator}');
    console.log('currentValue (Loops mus txog rau lub array, pib ntawm arr[1]): ${currentValue}');
    console.log('currentIndex: ${currentIndex}');
    console.log('array: ${array}');
    console.log('---');
    rov qab accumulator + currentValue;
  }
);

Tso zis

accumulator (Nyob rau 1 lub voj, accumulator yog arr[0], ces nws yog ib lub rov qab tus nqi ntawm yav tas los lub voj): 1
currentValue (Loops mus txog rau lub array, pib ntawm arr[1]): 2
currentIndex: 1
array: 1,2,3,4,5
---
accumulator (Nyob rau 1 lub voj, accumulator yog arr[0], ces nws yog ib lub rov qab tus nqi ntawm yav tas los lub voj): 3
currentValue (Loops mus txog rau lub array, pib ntawm arr[1]): 3
currentIndex: 2
array: 1,2,3,4,5
---
accumulator (Nyob rau 1 lub voj, accumulator yog arr[0], ces nws yog ib lub rov qab tus nqi ntawm yav tas los lub voj): 6
currentValue (Loops mus txog rau lub array, pib ntawm arr[1]): 4
currentIndex: 3
array: 1,2,3,4,5
---
accumulator (Nyob rau 1 lub voj, accumulator yog arr[0], ces nws yog ib lub rov qab tus nqi ntawm yav tas los lub voj): 10
currentValue (Loops mus txog rau lub array, pib ntawm arr[1]): 5
currentIndex: 4
array: 1,2,3,4,5
---

 

Muab thawj zaug nqi

Specifying lub thib ob parameter (initialValue) nyob rau hauv lub txo nuj nqi yuav ua rau initialValue yuav tsum tau dhau nyob rau hauv raws li lub accumulator sib cav nyob rau hauv thawj lub voj.

Nco ntsoov tias tam sim no tus nqi rau lub 1 voj tam sim no pib thaum thawj lub caij ntawm lub array

ua hauj lwm Piv txwv li

  cia arr = [1,2,3,4,5];

  arr.reduce(
  (accumulator, currentValue, currentIndex, array) => {
    console.log('accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): ${accumulator}');
    console.log('currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): ${currentValue}');
    console.log('currentIndex: ${currentIndex}');
    console.log('array: ${array}');
    console.log('---');
    rov qab accumulator + currentValue;
  }, 0
);

Tso zis

accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): 0
currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): 1
currentIndex: 0
array: 1,2,3,4,5
---
accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): 1
currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): 2
currentIndex: 1
array: 1,2,3,4,5
---
accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): 3
currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): 3
currentIndex: 2
array: 1,2,3,4,5
---
accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): 6
currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): 4
currentIndex: 3
array: 1,2,3,4,5
---
accumulator ( Nyob rau hauv 1 lub voj, qhov no yog qhov pib nqi, ces tus xa rov qab nqi ntawm yav tas los lub voj): 10
currentValue (Loops mus txog rau lub array, pib ntawm arr[0]): 5
currentIndex: 4
array: 1,2,3,4,5
---