Javascript data structure with map, reduce, filter and ES6 - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Javascript ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ, map, reduce, filter ๊ทธ๋ฆฌ๊ณ  ES6

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ถ€ํ„ฐ ์„ ์–ธ์  ์Šคํƒ€์ผ๊นŒ์ง€ ์–ด๋– ํ•œ ์Šคํƒ€์ผ์˜ ์ž‘์„ฑ๋„ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด์ค€ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ๋ช…๋ นํ˜•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ๋“ค์€ ๊ฐ์ฒด์ง€ํ–ฅ์–ธ์–ด๋ฅผ ๋ฐฐ๊ฒฝ์œผ๋กœ ์‹œ์ž‘ํ–ˆ๊ฑฐ๋‚˜, ๊ทธ๋“ค์€ ๊ทธ๊ฒƒ์„ ์‚ฌ๋ž‘ํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์Šคํƒ€์ผ์— ์ต์ˆ™ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ FP์ธ ์„ ์–ธ์  ์Šคํƒ€์ผ์„ ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆ„๊ธฐ ์ „์—, ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด์„œ ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ์ ์„ ์ดํ•ดํ•ฉ์‹œ๋‹ค. (๋งŒ์•ฝ์— ๋‹น์‹ ์ด ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๋ช‡ ๋ฌธ๋‹จ์€ ํŒจ์Šคํ•ด๋„๋ฉ๋‹ˆ๋‹ค.)

Imperative

// to calculate the sum of array elements
const sum = (arr) => {
  let result = 0; 
  for (let i = 0; i < arr.length; i++) {
    result += arr[i];
  }  
  return result;
};

๋ช…๋ นํ˜• ์Šคํƒ€์ผ์€ ๋ฉ‹์ง€์ง€๋งŒ ๋งŒ์•ฝ ๋ณต์žกํ•œ ์ˆ˜ํ•™ ๋…ผ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ–ˆ์„๋•Œ ์ฝ”๋“œ ํฌ๊ธฐ์™€ ๊ฐ€๋…์„ฑ์€ ํ˜•ํŽธ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ์ฝ์„ ๋•Œ ์ธ์ง€๋ถ€ํ•˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ์ถ”๋ก ๊ณผ ๋…ผ๋ฆฌ์— ์žˆ์–ด์„œ ๋” ์‰ฝ๊ฒŒ ํ—ค๋งค๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

๋˜ํ•œ, ์ด ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์˜ ์ฃผ์š” ๋ณต์žก์„ฑ์€ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์„ ์ปดํ“จํ„ฐ์— ๋งํ•˜๋Š” ๋Œ€์‹ , ์šฐ๋ฆฌ๊ฐ€ ๊ทธ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ์ง€์‹œํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์—์„œ ๋น„๋กฏ๋ฉ๋‹ˆ๋‹ค.

Declarative

// calculate the sum of array elements
const sum = (arr) => arr.reduce((total, item) => total += item, 0);

์ด์ œ,์ด ์ฝ”๋“œ๋Š” ๊ฝค ๊น”๋”ํ•˜๊ณ  ์งง์•„๋ณด์ด๊ณ  ์ „๋‹ฌ๋ ฅ์žˆ๊ณ  ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์—๋Ÿฌ๋ฅผ ์†ก์ถœํ• ๊ฑฐ ๊ฐ™์ง€ ์•Š๊ณ , ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์‰ฝ๊ณ  ๋””๋ฒ„๊น… ํ•˜๊ธฐ๋„ ์‰ฌ์›Œ๋ณด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ปดํ“จํ„ฐ์—๊ฒŒ ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฒƒ ๋Œ€์‹ ์— ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์„ ์–ธ๋ฌธ์€ ์ปดํŒŒ์ผ๋Ÿฌ ์ข…๋ฃŒ์‹œ์— ์‰ฝ๊ฒŒ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ ‘๊ทผํ•˜๊ณ  ์ ์€ ๋ถ€์ž‘์šฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Note: ๋งŒ์•ฝ์— ์œ„ ๋‘ ๊ฐœ์˜ ์„ฑ๋Šฅ๊ณผ ๋‹ค๋ฅธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜(map, reduce, filter, find)์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ๋ฐ˜๋“œ์‹œ ์ž‘์€ data set์„ ์œ„ํ•ด ์ด๊ฑธ ์ฝ๊ณ  large data set(100โ€“1000000)์„ ์œ„ํ•ด ์—ฌ๊ธฐ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

๋”์ด์ƒ ์ง€์ฒดํ•˜์ง€๋ง๊ณ , FP๋ฅผ ์œ„ํ•ด ์ œ์ผ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‹œ์ž‘ํ•ด๋ด…์‹œ๋‹ค.

Map

// definition 
collection.map((currentValue, index) => {
    // Return element for newArray
});
// example
const arr = [1,2,3,4,5];
const newArray = arr.map(i => i*10);
// return a new array with all value as multiple of 10;

Map ๋ฐฐ์—ด์—์„œ ์ž‘๋™ํ•˜๊ณ  ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์€ ์ง‘ํ•ฉ(์˜ˆ: ๋ฐฐ์—ด)์—์„œ ์ž‘๋™ํ•˜๋ฉฐ, ํ˜„์žฌ ๋ฐ˜๋ณต ๊ฐ’์„ ๊ฐ€์ง„ ์ฝœ๋ฐฑ์„ ์ธ์ˆ˜๋กœ์„œ ์ธ๋ฑ์Šคํ•˜๊ณ  ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Note: Map์€ ์ผ๋ถ€ ์กฐ๊ฑด์—์„œ๋Š” ํ๋ฆ„์„ ๋Š๋Š” ๋Œ€์‹  ์ „์ฒด ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝ/๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์ ํ•ฉํ•˜๋ฉฐ, map์˜ ์„ฑ๋Šฅ์„ ํ˜„๋ช…ํ•˜๊ฒŒ ํŒŒ์•…ํ• ๋ ค๋ฉด ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋Ÿฌ๋‚˜ ์ž‘์€ data sets์— ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.

Reduce

// definition 
collection.reduce((accumulator, item, index) => {
    // logic to perform to get accumulator as a return value
}, initialValue for accumulator);
// example
const arr = [1,2,3,4,5];
const total = arr.reduce((acc, item) => acc+= item, 0);
// return a total as 15

Reduce๋Š” ๋ฐฐ์—ด์—์„œ ์ž‘๋™ํ•˜์ง€๋งŒ ๋ฐ˜ํ™˜ ํ•  ์ˆ˜์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๋ฐ˜ํ™˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„ ์ž์ฒด๊ฐ€ ๋งํ•˜๋“ฏ์ด ๋ฌด์—‡์ด๋“  ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ map, find, filter ๋˜๋Š” ๋‹ค๋ฅธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ํ–‰๋™ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์€ ๋ฐฐ์—ด์—์„œ ์ž‘๋™ํ•˜๊ณ  ๋ฐฐ์—ด ํ•ญ๋ชฉ์˜ ์ด ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์ค„์ž…๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์ œ์˜ ์„ค๋ช… : ์ฒซ ๋ฒˆ์งธ ์ค„์„ ์ค„์ด๋ฉด acc์— 0 ๊ฐ’์ด ํ• ๋‹น๋˜๊ณ  acc += item ์ฆ‰ acc + acc + item์ด 0 + 1 1๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์ด 1์€ ๋‹ค์Œ ๋ฐ˜๋ณต ๋ฐ ๋ชจ๋“  ๋ฐฐ์—ด ํ•ญ๋ชฉ์„ ๋‹ค ๋๋‚ผ ๋•Œ๊นŒ์ง€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค.

Find

// definition 
collection.find((item) => {
    // return first element that satisfy the condition
});
// example
const arr = [1,2,8,4,5];
const value = arr.find(i => i%4 == 0);
// return the first value i.e 8 

Find๋Š” ๋ฐฐ์—ด์—์„œ ์ž‘๋™ํ•˜๊ณ  ํ•จ์ˆ˜์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Note: ์‰ฝ๊ณ  ๋‹จ์ˆœํ•˜์ง€๋งŒ ๋Œ€์šฉ๋Ÿ‰ data set์—์„œ๋Š” ํšจ์œจ์ ์ด์ง€ ์•Š์€ ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ์—ฌ๊ธธ ๋ณด์„ธ์š”

์ผ๋ถ€ ์‹ค์ œ ์‹œ๋‚˜๋ฆฌ์˜ค + ์ผ๋ถ€ ES6์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์•„๋ž˜์˜ ๊ฐ์ฒด ํ‚ค์—์„œ ARMD๋ฅผ ์‹œ๋„ํ•ด๋ณด์‹ญ์‹œ์˜ค)

ARMD๊ฐ€ Add, Read, Modify, Delete, ์ž์‹ ์˜ ํŠน์ˆ˜ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟจ๋งํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜„

const users = [
  {
    id: 1,
    name: "Jonathon Haley",
    username: "Monte.Weber2",
    email: "[email protected]",
    phone: "1-563-675-1857 x11708",
    website: "carmela.net",
    password: "hashed_password"
  },
  {
    id: 2,
    name: "Dean John",
    username: "dd.1",
    email: "[email protected]",
    phone: "1-123-543-1857 123212",
    website: "dd.net",
    password: "Dean_hashed_password"
  }
];

์ถ”๊ฐ€ ์˜ˆ์ œ๋ฅผ ์œ„ํ•ด ๋ฐฐ์—ด๋กœ users๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

1. ARMD - users์—๊ฒŒ ์ƒˆ๋กœ์šด ์š”์†Œ ์ถ”๊ฐ€

const newUser = {
    id: 4,
    name: "Denomer Crazy",
    username: "crazy.1",
    email: "[email protected]",
    phone: "",
    website: "crazy.app",
    password: "crazed_checker"
};
const newData = [...users, newUser]; // add element at last
or 
const newData = [newUser, ...users]; // add element at first
or 
const newData = users.concat(newUser) // the old way

ES6 ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž์˜ ์‚ฌ์šฉ์€ ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ๊ฐ€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ์••์ถ•ํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด์˜ ๋ชจ์–‘์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ๋™์  ํ‚ค ๊ฐ’ ์Œ ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const hobbies = ['chess', 'pool'];
const newUsers = users.map(u => ({...u, hobbies}))
// this will add hobbies to users array and return newUsers array

2. ARMD - users์˜ email, phone ๋ฐ website๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

const contactInfo = users.map(({email, website, phone}) => ({email, website, phone}));

๊ฐ์ฒด ํ‚ค์™€ map์˜ ๊ตฌ์กฐ๋ฅผ ํ•ด์ œํ•˜๋Š” es6์„ ์‚ฌ์šฉํ•˜์—ฌ users์˜ ์—ฐ๋ฝ์ฒ˜ ์ •๋ณด ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

3. ARMD - ๊ฐ์ฒด ํ‚ค ๊ฐ’ ์ฐพ๊ธฐ ๋ฐ ๋ฐ”๊พธ๊ธฐ

const newUsers = users.map(u => u.id == 2? ({...u, name: 'te'}): u);
// this will return newUsers with all user having name 'te'

3. ARMD - ๊ฐ์ฒด์—์„œ ์ผ๋ถ€ ํ‚ค ์‚ญ์ œ

Note : ์‹ค์ œ๋กœ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ  ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ‚ค ์‚ญ์ œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด ์—ฌ๊ธฐ์—์„œ ๊ฐ์ฒด ๋ถˆ๋ณ€์„ฑ์„ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ๊ฐ€์žฅ ์‰ฝ๊ณ  ๋‹จ์ผ ๋ผ์ธ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. users๋กœ๋ถ€ํ„ฐ website๋ฅผ ์‚ญ์ œํ•ด๋ด…์‹œ๋‹ค.

const newUsers = users.map({id, email, name, username, phone, password} => ({id, email, username, email, phone, password}));
// will return an array with all keys other than website

์œ„์˜ ์ฝ”๋“œ๋Š” ํฐ ๊ฐ์ฒด๋ฅผ ์œ„ํ•ด ์ฝ”๋“œํ™” ํ•˜๊ธฐ๊ฐ€ ์‚ฌ์‹ค์ƒ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const newUsers = users.map(u => Object.keys(u).reduce((newObj, key) => key != 'website' ? { ...newObj, [key]: u[key]} : newObj, {}));

์šฐ๋ฆฌ๋Š”users๋ฅผ ํ†ตํ•ด map์„ํ•˜๊ณ , ๊ฐ๊ฐ์˜user์—reduce๋ฅผํ•˜๊ณ  new object(newObj)๋ฅผ ๋งŒ๋“  ๋‹ค์Œ website ํ‚ค๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ website๊ฐ€ ์ด์ „์— ํ˜•์„ฑ๋œ ๊ฒƒ์€ newObj๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  obj์— require ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋งˆ์นจ๋‚ด newObj๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด์ „์— ํ˜•์„ฑ๋œ newObj๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด, ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  obj์— require ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋งˆ์นจ๋‚ด newObj๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํŒ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค! ๐Ÿ’ฐ

๋‚ด Bitcoin ์ฃผ์†Œ : 132Ndcy1ZHs6DU4pV3q2X1GzSCdBEXX6pz
My Ethereum ์ฃผ์†Œ : 0xc46204dfc8449Ffb0f02a9e1aD81F30D3f027010

๋‚ด ์ด๋ฉ”์ผ ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•˜๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ github์—์„œ ๋” ๋งŽ์€ ๊ธฐ์‚ฌ๋ฅผ ์ฝ์œผ๋ ค๋ฉด ๋„ˆ์˜ ์ด๋ฉ”์ผ์„ ์—ฌ๊ธฐ ์— ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค. ๋‚ด ๋ฏธ์นœ ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

๋‚˜์˜ ๋‹ค๋ฅธ ๊ธฐ์‚ฌ๋ฅผ ์ข‹์•„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. Nodejs app structure
  2. Javascript ES6- Iterables and Iterators
  3. Javascript generator-yield/next & async-await

์ด ๊ฒŒ์‹œ๋ฌผ์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ์•„๋ž˜์˜ ๐Ÿ‘ ๋ฐ•์ˆ˜ ์น˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ง€์›์„ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค! ๋˜ํ•œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ์ถ”์ฒœํ•˜๊ณ  ๊ณต์œ ํ•˜์‹ญ์‹œ์˜ค!

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!