JavaScript

map, filter & reduce

Në shqip: Metodat e vargjeve

ShpjegimiSQ

map(), filter() dhe reduce() janë metodat më të përdorura të vargjeve në JavaScript: map e ndryshon çdo element, filter mban vetëm ata që plotësojnë një kusht, reduce i bashkon të gjithë në një vlerë (p.sh. shumën).

EnglishEN

map(), filter() and reduce() are JavaScript's most-used array methods: map transforms every item, filter keeps only those that pass a test, reduce combines them all into one value (like a sum).

Si ta mendosh

Si në kuzhinë me një shportë mollësh: map — i qëron të gjitha; filter — mban vetëm të kuqet; reduce — i bën të gjitha një kavanoz reçel.

Lexoje në anglisht

Like a basket of apples in the kitchen: map — peel them all; filter — keep only the red ones; reduce — turn them all into one jar of jam.

Shembull kodijavascript

const cmimet = [2, 15, 8, 30];
const meTVSH = cmimet.map((c) => c * 1.18);
const teLira = cmimet.filter((c) => c < 10);         // [2, 8]
const totali = cmimet.reduce((sh, c) => sh + c, 0);  // 55

Terma të lidhur