JavaScript Vargjeve : Të kuptuarit e reduktuar funksionin
Sintaksë
arr.reduce(callbackFunction, [initialValue])
Çfarë do të zvogëlojë të bëni?
Ulja është një funksion që unazore me një rrjet.
Operacionet mund të kryhet në grup nëpërmjet një funksion callback.
Në fund të operacioneve në grup, reduktuar kthen një vlerë të vetme
Reduce can be used, for example, to total a shopping cart
shembull
le arr = [1,2,3,4,5];
arr.reduce( (akumulator, vlera e tanishme, currentIndex, grup) => {
//disa operacione;
kthehen diçka;
});
Callback Funksioni
Funksioni callback thirret për çdo element në vektorit, duke kaluar në 4 Argumentet të cilat janë miratuar në nga funksioni të reduktuar.
këto 4 argumente pastaj mund të përdoret në funksion callback tonë.
Merr katër argumente
- akumulator – Elementi i parë i vektorit në lak e parë ose vlerën fillestare, nëse furnizohen. Atëherë vlera kthye e lak e mëparshme.
- vlera e tanishme – Elementi i tanishëm në rrjet. 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 – Indeksi i elementit aktual.
- grup – E gjithë array.
Punës Shembull
le arr = [1,2,3,4,5]; arr.reduce( (akumulator, vlera e tanishme, currentIndex, grup) => { console.log(`akumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): ${akumulator}`); console.log(`currentValue (Loops through the array, starting at arr[1]): ${vlera e tanishme}`); console.log(`currentIndex: ${currentIndex}`); console.log(`array: ${grup}`); console.log('---'); akumulator kthimit + vlera e tanishme; } );
prodhim
akumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 1 vlera e tanishme (Loops through the array, starting at arr[1]): 2 currentIndex: 1 grup: 1,2,3,4,5 --- akumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 3 vlera e tanishme (Loops through the array, starting at arr[1]): 3 currentIndex: 2 grup: 1,2,3,4,5 --- akumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 6 vlera e tanishme (Loops through the array, starting at arr[1]): 4 currentIndex: 3 grup: 1,2,3,4,5 --- akumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 10 vlera e tanishme (Loops through the array, starting at arr[1]): 5 currentIndex: 4 grup: 1,2,3,4,5 ---
Sigurimi i vlerës fillestare
Specifikimi parametrin e dytë (initialValue) in the reduce function will cause initialValue to be passed in as the accumulator argument on the first loop.
Note that the current value on the 1st loop now starts at the first element of the array
Punës Shembull
le arr = [1,2,3,4,5]; arr.reduce( (akumulator, vlera e tanishme, currentIndex, grup) => { console.log(`akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): ${akumulator}`); console.log(`currentValue (Loops through the array, starting at arr[0]): ${vlera e tanishme}`); console.log(`currentIndex: ${currentIndex}`); console.log(`array: ${grup}`); console.log('---'); akumulator kthimit + vlera e tanishme; }, 0 );
prodhim
akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 0 vlera e tanishme (Loops through the array, starting at arr[0]): 1 currentIndex: 0 grup: 1,2,3,4,5 --- akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 1 vlera e tanishme (Loops through the array, starting at arr[0]): 2 currentIndex: 1 grup: 1,2,3,4,5 --- akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 3 vlera e tanishme (Loops through the array, starting at arr[0]): 3 currentIndex: 2 grup: 1,2,3,4,5 --- akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 6 vlera e tanishme (Loops through the array, starting at arr[0]): 4 currentIndex: 3 grup: 1,2,3,4,5 --- akumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 10 vlera e tanishme (Loops through the array, starting at arr[0]): 5 currentIndex: 4 grup: 1,2,3,4,5 ---
