Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / JavaScript

Array.prototype.reduceRight: zmanjšanje od konca

reduceRight združi obstoječe elemente v eno vrednost in obiskuje indekse od desne proti levi.

Različica: ECMAScript 2026 draft; example target: Node.js 24.22.2

Sintaksa

array.reduceRight(callbackFn[, initialValue])

array.reduceRight(callbackFn[, initialValue]) kliče callbackFn(accumulator, currentValue, index, array) za obstoječe indekse padajoče in vrne končni akumulator. Z initialValue začne s to vrednostjo. Brez nje zadnji obstoječ element postane akumulator in ni podan kot currentValue.

Prazna mesta preskoči. Prazna tabela ali tabela samo s praznimi mesti brez initialValue sproži TypeError; izrecni undefined šteje kot začetna vrednost. Dolžina se zajame pred iteracijo: poznejši dodatek za njo je prezrt, indeks, izbrisan pred obiskom, pa preskočen. Metoda sama ne mutira in je splošna za array-like; callback, ki ni funkcija, prejemnik null ali undefined in napake callbacka sprožijo izjemo.

Primeri

const sparse = [1, , 3, 4];
const steps = [];
const total = sparse.reduceRight((accumulator, value, index) => {
  steps.push(index + ":" + accumulator + "-" + value);
  return accumulator - value;
}, 20);
console.log(total);
console.log(steps.join("|"));
console.log([, 5, ,].reduceRight((a, b) => a + b));
try { new Array(2).reduceRight((a, b) => a + b); } catch (error) { console.log(error.name); }

Rezultat

12
3:20-4|2:16-3|0:13-1
5
TypeError

Viri