JavaScript iri-iri : Fahimtar rage aiki
ginin kalma
arr.reduce(callbackFunction, [initialValue])
Menene Rage yi?
Rage ne mai aiki da madaukai ta hanyar wani tsararru.
Ayyuka za a iya yi a kan tsararru via wani callback aiki.
A karshen ayyukan a kan tsararru, rage ya dawo guda darajar
Rage za a iya amfani da, misali, to total wani Siyayya
misali
bari arr = [1,2,3,4,5];
arr.reduce( (accumulator, currentValue, currentIndex, tsararru) => {
//wasu ayyukan;
komawa wani abu;
});
The Callback Function
The callback function is invoked for each element in the array, passing in 4 arguments which are passed in by the reduce function.
These 4 arguments then can be used in our callback function.
Takes four arguments
- accumulator – The first element of the array on the first loop or initial value if supplied. Then the returned value of the previous loop.
- currentValue – A halin yanzu kashi a cikin tsararru. Starts from the second element in the array ie. arr[1], when the initial value argument is not passed. If the initial value argument is passed then current value starts from the 1st element in the array arr[0].
- currentIndex – A index of yanzu kashi.
- tsararru – A dukan tsararru.
Working Example
bari arr = [1,2,3,4,5]; arr.reduce( (accumulator, currentValue, currentIndex, tsararru) => { console.log(`accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): ${accumulator}`); console.log(`currentValue (Loops through the array, starting at arr[1]): ${currentValue}`); console.log(`currentIndex: ${currentIndex}`); console.log(`tsararru: ${tsararru}`); console.log('---'); samu accumulator + currentValue; } );
Output
accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 1 currentValue (Loops through the array, starting at arr[1]): 2 currentIndex: 1 tsararru: 1,2,3,4,5 --- accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 3 currentValue (Loops through the array, starting at arr[1]): 3 currentIndex: 2 tsararru: 1,2,3,4,5 --- accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 6 currentValue (Loops through the array, starting at arr[1]): 4 currentIndex: 3 tsararru: 1,2,3,4,5 --- accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 10 currentValue (Loops through the array, starting at arr[1]): 5 currentIndex: 4 tsararru: 1,2,3,4,5 ---
Samar da farko darajar
Tantance na biyu siga (initialValue) a rage aiki zai haifar da initialValue da za a wuce a matsayin accumulator shaida a kan na farko madauki.
Ka lura da cewa a halin yanzu darajar a kan 1st madauki yanzu yana farawa a farko kashi na tsararru
Working Example
bari arr = [1,2,3,4,5]; arr.reduce( (accumulator, currentValue, currentIndex, tsararru) => { console.log(`accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): ${accumulator}`); console.log(`currentValue (Loops through the array, starting at arr[0]): ${currentValue}`); console.log(`currentIndex: ${currentIndex}`); console.log(`tsararru: ${tsararru}`); console.log('---'); samu accumulator + currentValue; }, 0 );
Output
accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): 0 currentValue (Loops through the array, starting at arr[0]): 1 currentIndex: 0 tsararru: 1,2,3,4,5 --- accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): 1 currentValue (Loops through the array, starting at arr[0]): 2 currentIndex: 1 tsararru: 1,2,3,4,5 --- accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): 3 currentValue (Loops through the array, starting at arr[0]): 3 currentIndex: 2 tsararru: 1,2,3,4,5 --- accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): 6 currentValue (Loops through the array, starting at arr[0]): 4 currentIndex: 3 tsararru: 1,2,3,4,5 --- accumulator ( A cikin 1st madauki, wannan shi ne na farko da darajar, sa'an nan suka komo daga darajar baya madauki): 10 currentValue (Loops through the array, starting at arr[0]): 5 currentIndex: 4 tsararru: 1,2,3,4,5 ---
