Array Javascript zmenšit ()

Metoda JavaScript Array redukovat () provede funkci redukce na každém prvku pole a vrátí jednu výstupní hodnotu.

Syntaxe reduce()metody je:

 arr.reduce(callback(accumulator, currentValue), initialValue)

Zde je arr pole.

redukovat () parametry

reduce()Metoda bere v:

  • zpětné volání - funkce, která se má provést na každém prvku pole (kromě prvního prvku, pokud není k dispozici počáteční hodnota). Zabere to
    • akumulátor - akumuluje návratové hodnoty zpětného volání.
    • currentValue - aktuální prvek předávaný z pole.
  • initialValue (volitelně) - Hodnota, která bude předána callback()při prvním volání. Pokud není k dispozici, první prvek funguje jako akumulátor prvního volání a callback()nebude se na něm provádět.

Poznámka: Volání reduce()na prázdné pole bez initialValue vyvolá TypeError.

Návratová hodnota z redukovat ()

  • Vrátí jedinou hodnotu vzniklou po zmenšení pole.

Poznámky :

  • reduce() provede danou funkci pro každou hodnotu zleva doprava.
  • reduce() nezmění původní pole.
  • Poskytování je téměř vždy bezpečnější initialValue.

Příklad 1: Součet všech hodnot pole

 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ýstup

 21 21

Příklad 2: Odečítání čísel v poli

 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ýstup

 1330 2700

Tento příklad jasně vysvětluje rozdíl mezi předáním initialValue a nepředáním initialValue.

Příklad 3: Odebrání duplicitních položek z pole

 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ýstup

 (18, 21, 1, 51, 5, 7, 10)

Příklad 4: Seskupení objektů podle vlastnosti

 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ýstup

 ('19': ((jméno: 'Dwight', věk: 19)), '21': ((jméno: 'John', věk: 21), (jméno: 'Oscar', věk: 21)), '' 55 ': ((jméno:' Oliver ', věk: 55), (jméno:' Michael ', věk: 55), (jméno:' Kevin ', věk: 55)))

Doporučené čtení: JavaScript Array reduRight ()

Zajímavé články...