Metoda JavaScript Array reduce () izvaja funkcijo reduktorja za vsak element matrike in vrne eno izhodno vrednost.
Sintaksa reduce()
metode je:
arr.reduce(callback(accumulator, currentValue), initialValue)
Tu je arr matrika.
reduce () Parametri
reduce()
Metoda je v:
- povratni klic - funkcija za izvajanje na vsakem elementu matrike (razen prvega elementa, če ni podana začetna vrednost). Vzame
- akumulator - zbira povratne vrednosti povratnega klica.
- currentValue - trenutni element, ki se prenaša iz polja.
- začetna vrednost (neobvezno) - vrednost, ki bo posredovana
callback()
ob prvem klicu. Če ni na voljo, prvi element deluje kot akumulator prvega klica incallback()
se na njem ne bo izvedel.
Opomba: Klic reduce()
praznega polja brez začetne vrednosti bo vrgel TypeError
.
Vrnjena vrednost iz zmanjšanja ()
- Vrne posamezno vrednost, ki nastane po zmanjšanju polja.
Opombe :
reduce()
izvrši dano funkcijo za vsako vrednost od leve proti desni.reduce()
ne spremeni prvotnega polja.- Skoraj vedno je varneje zagotoviti
initialValue
.
Primer 1: Vsota vseh vrednosti matrike
const numbers = (1, 2, 3, 4, 5, 6); function sum_reducer(accumulator, currentValue) ( return accumulator + currentValue; ) let sum = numbers.reduce(sum_reducer); console.log(sum); // 21 // using arrow function let summation = numbers.reduce( (accumulator, currentValue) => accumulator + currentValue ); console.log(summation); // 21
Izhod
21. 21.
2. primer: odštevanje števil v matriki
const numbers = (1800, 50, 300, 20, 100); // subtract all numbers from first number // since 1st element is called as accumulator rather than currentValue // 1800 - 50 - 300 - 20 - 100 let difference = numbers.reduce( (accumulator, currentValue) => accumulator - currentValue ); console.log(difference); // 1330 const expenses = (1800, 2000, 3000, 5000, 500); const salary = 15000; // function that subtracts all array elements from given number // 15000 - 1800 - 2000 - 3000 - 5000 - 500 let remaining = expenses.reduce( (accumulator, currentValue) => accumulator - currentValue, salary ); console.log(remaining); // 2700
Izhod
1330 2700
Ta primer jasno pojasnjuje razliko med posredovanjem začetne vrednosti in nenastavitvijo začetne vrednosti.
3. primer: odstranite podvojene elemente iz polja
let ageGroup = (18, 21, 1, 1, 51, 18, 21, 5, 18, 7, 10); let uniqueAgeGroup = ageGroup.reduce(function (accumulator, currentValue) ( if (accumulator.indexOf(currentValue) === -1) ( accumulator.push(currentValue); ) return accumulator; ), ()); console.log(uniqueAgeGroup); // ( 18, 21, 1, 51, 5, 7, 10 )
Izhod
(18, 21, 1, 51, 5, 7, 10)
Primer 4: Združevanje predmetov po lastnostih
let people = ( ( name: "John", age: 21 ), ( name: "Oliver", age: 55 ), ( name: "Michael", age: 55 ), ( name: "Dwight", age: 19 ), ( name: "Oscar", age: 21 ), ( name: "Kevin", age: 55 ), ); function groupBy(objectArray, property) ( return objectArray.reduce(function (accumulator, currentObject) ( let key = currentObject(property); if (!accumulator(key)) ( accumulator(key) = (); ) accumulator(key).push(currentObject); return accumulator; ), ()); ) let groupedPeople = groupBy(people, "age"); console.log(groupedPeople);
Izhod
('19': ((ime: 'Dwight', starost: 19)), '21': ((ime: 'John', starost: 21), (ime: 'Oscar', starost: 21)), ' 55 ': ((ime:' Oliver ', starost: 55), (ime:' Michael ', starost: 55), (ime:' Kevin ', starost: 55)))
Priporočeno branje: Array JavaScript reduceRight ()