[JavaScript ES6+] map, filter, reduce

Yujin Leeยท2021๋…„ 5์›” 5์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
10/14
post-thumbnail

์‚ฌ์ „์ค€๋น„



1. map

๋ฐ์ดํ„ฐ์—์„œ ์ด๋ฆ„์„ ์ถ”์ถœํ•  ๋•Œ

// ๋ช…๋ นํ˜• ์ฝ”๋“œ
// ์ด๋ฆ„๋งŒ ๋นผ๋‚ด๊ธฐ
let names = [];
for (const p of products) {
   names.push(p.name);
}
log(names);

๋ฐ์ดํ„ฐ์—์„œ ๋‚˜์ด๋ฅผ ๊ฐ€๊ฒฉ์„ ์ถ”์ถœํ•  ๋•Œ

let prices = [];
for (const p of products) {
   names.push(p.price);
}
log(prices);

๐Ÿ‘†๐Ÿป ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•œ๋‹ค.
์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด map

const map = (f, iter) => {
    let res = [];
    for (const a of iter) {
        res.push(f(a));
    }
    return res;
};
    
log(map(p => p.name, products));
log(map(p => p.price, products));

map




2. ์ดํ„ฐ๋Ÿฌ๋ธ” ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅธ map์˜ ๋‹คํ˜•์„ฑ

log(document.querySelectorAll('*').map);  // -> undefined

document.querySelectorAll์€ array๋ฅผ ์ƒ์†๋ฐ›์€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ์„œ prototype์— mapํ•จ์ˆ˜๊ฐ€ ๊ตฌํ˜„์ด ์•ˆ๋˜์–ด์žˆ์Œ

log([1, 2, 3].map(a => a + 1));


log(map(el => el.nodeName, document.querySelectorAll('*')));

const it = document.querySelectorAll('*')[Symbol.iterator]();
log(it.next());
log(it.next());
log(it.next());
log(it.next());
log(it.next());


function *gen() { //์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ƒ์„ฑ
    yield 2;
    if (false) yield 3;
    yield 4;
}

log(map(a => a * a, gen()));


+

let m = new Map();
m.set('a', 10);
m.set('b', 20);
log(new Map(map(([k, a]) => [k, a * 2], m)));




3. filter

// ๋ช…๋ นํ˜• ์ฝ”๋“œ
let under20000 = [];
for (const p of products) {
    if (p.price < 20000) under20000.push(p);
}
log(...under20000);

๋‹ค๋ฅธ ์กฐ๊ฑด์„ ๋งŒ๋“œ๋ ค๋ฉด ์ฝ”๋“œ ๋ณต์‚ฌํ•ด์•ผ ๋˜์„œ ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ
filter๋กœ ๋ฆฌํŒฉํ† ๋ง ํ•œ๋‹ค.

const filter = (f, iter) => { //f๊ฐ€ ํ•จ์ˆ˜์ž„(๊ฑธ๋Ÿฌ๋‚ผ ์กฐ๊ฑด!)
    let res = [];
    for (const a of iter) {
        if (f(a)) res.push(a);
    }
    return res;
};

log(...filter(p => p.price < 20000, products));


log(...filter(p => p.price >= 20000, products));


log(filter(n => n % 2, [1, 2, 3, 4]));


log(filter(n => n % 2, function* () {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
}()));




4. reduce

reduce : ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ’์„ ์ถ•์•ฝํ•˜๋Š” ํ•จ์ˆ˜

๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• 1

const reduce = () => {

};

log(reduce());

๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• 2

const add = (a, b) => a + b;

log(reduce(add, 0, [1, 2, 3, 4, 5])); // -> 15
log(add(add(add(add(add(0, 1), 2), 3), 4), 5)); // -> 15
log(reduce(add, [1, 2, 3, 4, 5])); // -> 15

const reduce = (f, acc, iter) => {
    if (!iter) {
        iter = acc[Symbol.iterator]();
        acc = iter.next().value;
    }
    for (const a of iter) {
        acc = f(acc, a);
    }
    return acc;
};

log(reduce());

products์˜ ๋ชจ๋“  price๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ์„ reduce๋งŒ ์จ์„œ ๊ตฌํ˜„ํ•˜๋ฉด

log(reduce(
    (total_price, product) =>
      total_price + product.price, 0, products));



5. map + filter + reduce ์ค‘์ฒฉ ์‚ฌ์šฉ๊ณผ ํ•จ์ˆ˜ํ˜• ์‚ฌ๊ณ 

์‚ฌ์ „์ค€๋น„ - fx.jsํŒŒ์ผ ์ƒ์„ฑ

์—ฐ๋™

<script src="../lib/fx.js"></script>

ex) 20,000์› ๋ฏธ๋งŒ์˜ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ๋ชจ๋‘ ํ•ฉ์น˜๊ธฐ

const add = (a, b) => a + b;

log( //์ถœ๋ ฅํ• ๊ฑฐ์•ผ
    reduce( //๊ฐ’์„ ํ•จ์ˆ˜์— ๋„ฃ์–ด์„œ ์ถ•์•ฝํ• ๊ฑด๋ฐ
        add, //๊ทธ ํ•จ์ˆ˜๋Š” add์ด๊ณ  ๊ฐ’์€ ๋ฐ์ดํ„ฐ์—์„œ ์ถ”์ถœ(map)ํ•ด๋‚ผ๊ฑฐ์•ผ
        map(p => p.price, //๋ญ˜ ์ถ”์ถœํ• ๊ฑฐ๋ƒ๋ฉด ๊ฐ€๊ฒฉ
            filter(p => p.price < 20000, products)))); //๊ทผ๋ฐ ๊ฐ€๊ฒฉ์ด 20000์› ๋ฏธ๋งŒ์ธ ๊ฒƒ์„ ํ•„ํ„ฐ๋งํ• ๊ฑฐ์•ผ

ex) 20,000์› ์ด์ƒ์˜ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ๋ชจ๋‘ ํ•ฉ์น˜๊ธฐ

log(
    reduce(
        add,
        filter(n => n >= 20000,
            map(p => p.price, products))));

profile
I can be your Genie๐Ÿงžโ€โ™€๏ธ How โ€˜bout Aladdin? ๐Ÿงžโ€โ™‚๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€