JavaScripti massiivi vähendamise () meetod täidab massiivi iga elemendi reduktori funktsiooni ja tagastab ühe väljundväärtuse.
Meetodi süntaks reduce()
on:
arr.reduce(callback(accumulator, currentValue), initialValue)
Siin on arr massiiv.
vähendada () parameetrid
reduce()
Meetod võtab:
- tagasihelistamine - funktsioon, mida käivitada igal massiivi elemendil (välja arvatud esimene element, kui esialgset väärtust pole ette nähtud). See võtab sisse
- akumulaator - see kogub tagasihelistamise tagasivõtuväärtused.
- currentValue - massiivist edastatav praegune element.
- initialValue (valikuline) - väärtus, mis edastatakse
callback()
esimesel kõnel . Kui seda pole ette nähtud, toimib esimene element esimese kõne akumulaatorina ja sedacallback()
ei täideta.
Märkus:reduce()
tühja massiivi kutsumine ilma algväärtuseta viskab TypeError
.
Tagastusväärtus redutseerimisest ()
- Tagastab pärast massiivi vähendamist saadud ühe väärtuse.
Märkused :
reduce()
täidab iga väärtuse jaoks antud funktsiooni vasakult paremale.reduce()
ei muuda algset massiivi.- Seda on peaaegu alati ohutum pakkuda
initialValue
.
Näide 1: Massiivi kõigi väärtuste summa
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
Väljund
21 21
Näide 2: Massiivi numbrite lahutamine
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
Väljund
1330 2700
See näide selgitab selgelt erinevust algväärtuse ja algväärtuse edastamata jätmise vahel.
Näide 3: eemaldage massiivist dubleerivad üksused
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 )
Väljund
(18, 21, 1, 51, 5, 7, 10)
Näide 4: Objektide rühmitamine atribuudi järgi
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);
Väljund
('19': ((nimi: 'Dwight', vanus: 19)), '21': ((nimi: 'John', vanus: 21), (nimi: 'Oscar', vanus: 21)), ' 55 ': ((nimi:' Oliver ', vanus: 55), (nimi:' Michael ', vanus: 55), (nimi:' Kevin ', vanus: 55)))
Soovitatav lugemine: JavaScripti massiiv reducRight ()