i-JavaScript afanayo : Ukuqonda ukunciphisa umsebenzi

syntax

arr.reduce(callbackFunction, [initialValue])

Kusho ukunciphisa ukwenza?

Nciphisa umsebenzi loops ngokusebenzisa array.

Operations engenziwa amalungu afanayo nge-callback.

Ekupheleni imisebenzi ku array, ukunciphisa ukubuyiselwa inani elilodwa

Nciphisa ingasetshenziswa, Ngokwesibonelo, ingqikithi inqola yokuthenga

isibonelo

  ake Fika = [1,2,3,4,5];
  arr.reduce( (accumulator, currentValue, currentIndex, amalungu afanayo) => { 
      //eminye imisebenzi;
      ubuyele into;
  });

 

I Phinda ukushayela Umsebenzi

Callback Lusetshenziswa for element ngayinye kumalungu afanayo, labona 4 agumenti zazo ophasiswe yi ukunciphisa umsebenzi.

Lezi 4 agumenti ke ingasetshenziswa-callback yethu umsebenzi.

Esithatha izimpikiswano ezine

  1. accumulator – I element lokuqala afanayo ku-loop lokuqala noma ukubaluleka kokuqala uma enikeziwe. Khona-ke inani elibuyisiwe loop odlule.
  2. currentValue – I element samanje afanayo. Iqala kusukela Isici sesibili kule ie afanayo. Fika[1], lapho kokuqala value agumenti awuphasiswa. Uma kokuqala value agumenti sidlulile ukubaluleka ke wamanje iqala kusukela isici 1st in the Fika afanayo[0].
  3. currentIndex – Inkomba isici samanje.
  4. amalungu afanayo – Lonke afanayo.

Ukusebenza Isibonelo

ake Fika = [1,2,3,4,5];

arr.reduce(
  (accumulator, currentValue, currentIndex, amalungu afanayo) => {
    console.log(`accumulator (On iluphu 1st, accumulator kuyinto Fika[0], khona-ke inani elibuyisiwe loop odlule): ${accumulator}`);
    console.log(`currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[1]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`afanayo: ${amalungu afanayo}`);
    console.log('---');
    ukubuya accumulator + currentValue;
  }
);

lokukhipha

accumulator (On iluphu 1st, accumulator kuyinto Fika[0], khona-ke inani elibuyisiwe loop odlule): 1
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[1]): 2
currentIndex: 1
amalungu afanayo: 1,2,3,4,5
---
accumulator (On iluphu 1st, accumulator kuyinto Fika[0], khona-ke inani elibuyisiwe loop odlule): 3
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[1]): 3
currentIndex: 2
amalungu afanayo: 1,2,3,4,5
---
accumulator (On iluphu 1st, accumulator kuyinto Fika[0], khona-ke inani elibuyisiwe loop odlule): 6
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[1]): 4
currentIndex: 3
amalungu afanayo: 1,2,3,4,5
---
accumulator (On iluphu 1st, accumulator kuyinto Fika[0], khona-ke inani elibuyisiwe loop odlule): 10
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[1]): 5
currentIndex: 4
amalungu afanayo: 1,2,3,4,5
---

 

Ukunikeza ukubaluleka kokuqala

Ecacisa Ipharamitha yesibili (initialValue) ku ukunciphisa umsebenzi kuzokwenza initialValue sidluliselwa njengo-agumenti accumulator ku-loop lokuqala.

Qaphela ukuthi ukubaluleka lamanje loop 1st manje uqala ngo element lokuqala afanayo

Ukusebenza Isibonelo

  ake Fika = [1,2,3,4,5];

  arr.reduce(
  (accumulator, currentValue, currentIndex, amalungu afanayo) => {
    console.log(`accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): ${accumulator}`);
    console.log(`currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`afanayo: ${amalungu afanayo}`);
    console.log('---');
    ukubuya accumulator + currentValue;
  }, 0
);

lokukhipha

accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): 0
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): 1
currentIndex: 0
amalungu afanayo: 1,2,3,4,5
---
accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): 1
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): 2
currentIndex: 1
amalungu afanayo: 1,2,3,4,5
---
accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): 3
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): 3
currentIndex: 2
amalungu afanayo: 1,2,3,4,5
---
accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): 6
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): 4
currentIndex: 3
amalungu afanayo: 1,2,3,4,5
---
accumulator ( On loop 1st, lena ukubaluleka kokuqala, ke inani elibuyisiwe loop odlule): 10
currentValue (Loops ngokusebenzisa afanayo, ukuqala at Fika[0]): 5
currentIndex: 4
amalungu afanayo: 1,2,3,4,5
---                                                                 

Shiya impendulo