10 Array Functions for HOF๐Ÿบ

1. ๊ณ ์ฐจ ํ•จ์ˆ˜๐Ÿคฌ

๊ตฌ๊ธ€ ์บ˜๋ฆฐ๋”์— ์ดํ‹€ ์ „ ๊ธˆ์š”์ผ ์บ˜๋ฆฐ๋”์˜ ๋‚ด์šฉ์€ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ˜ํƒˆ์ด ํƒˆํƒˆ ํ„ธ๋ ค๋ฒ„๋ ธ๋‹ค. ํŽ˜์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ๋ฐ ๋‚ด๊ฐ€ ํŽ˜์–ด๋‹˜์—๊ฒŒ ์ „ํ˜€ ๋„์›€์„ ๋“œ๋ฆฌ์ง€ ๋ชปํ•  ๋ฟ๋”๋Ÿฌ ํ•˜๋‚˜๋„ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋”๋ผ. ์ฃผ๋ง์— ๊ณต๋ถ€ํ•ด ๋ณด์ž ํ•˜๋ฉด์„œ๋„ ์†์ƒํ•ดํ–ˆ๋‹ค.

2. ์‚ฌ์ „ ํ•™์Šต - ๋จผ์ € ์•Œ์•„์•ผ ์ข‹์€ ๊ฒƒ๋“ค๐ŸŒป

์˜ค๋Š˜ ํ•œ 2์‹œ ๋ฐ˜ ๋ถ€ํ„ฐ ํ•™์Šต๋‹ค์šด ํ•™์Šต์„ ์‹œ์ž‘ํ–ˆ๋‹ค. ๋‚˜์˜ ํŽ˜์–ด๋‹˜๊ป˜์„œ ์ ์‹ฌ์‹œ๊ฐ„ ๋•Œ์ฏค โ€˜๋“œ๋ฆผ์ฝ”๋”ฉ ์—˜๋ฆฌโ€™ ๋ผ๋Š” ์œ ํŠœ๋ฒ„๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๋“ค์„ ๊ฒƒ์„ ์ถ”์ฒœํ•ด ์ฃผ์…จ๋‹ค.

https://www.youtube.com/watch?v=3CUjtKJ7PJg&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=9

์œ ์šฉํ•œ 10๊ฐ€์ง€ ๋ฐฐ์—ด ํ•จ์ˆ˜๋“ค์— ๊ด€ํ•˜์—ฌ ๋ฐฐ์šด๋‹ค๋Š” ์ œ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์›ฌ์ง€ ๋Š๋‚Œ์ƒ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ด์•ผ ์˜์ƒ์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค ์‹ถ์–ด ์•ž์ „์˜ 8๊ฐœ ๊ฐ•์˜๋ฅผ ๋ชจ์กฐ๋ฆฌ ๋“ค์–ด๋ฒ„๋ ธ๋‹ค. dream coding

๊ฒฐ๋ก ์€ ๋ชจ์กฐ๋ฆฌ ๋“ค์–ด๋ฒ„๋ฆฐ๊ฒŒ ๋ณต์Šต ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ณธ๊ฒŒ์ž„์„ ๋” ์ œ๋Œ€๋กœ ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด๋ฒ„๋ ธ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ์‚ฌํ•ญ์„ ๋‚จ๊ฒจ๋ดค๋‹ค.

  1. Rest Paramenters

๋งˆ์นจ Koans-08 ์ฃผ์ œ๊ฐ€ Rest Paramenters ๊ด€ํ•œ ๊ฑฐ์ด๊ธฐ๋„ ํ–ˆ๊ณ  ๋ณต์Šตํ•˜๋Š” ๊ณผ์ •์—์„œ ๋”ฑ ๊ทธ๊ฑฐ๋งŒ ๋‚จ๊ฒจ๋‘” ์ƒํƒœ์˜€๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์‹œ ํ’€์–ด๋ดค๋‹ค. ๋งค์šฐ ๋„์›€์ด ๋˜์—ˆ๋‹ค.

  1. ํ•จ์ˆ˜ ํ‘œํ˜„์‹๊ณผ ํ•จ์ˆ˜ ์„ ์–ธ์‹์˜ ์ฐจ์ด
  2. ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๋‹ค์Œ ์ค„์— ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  3. ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์ด์ „์—์„œ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…์ด ๊ฐ€๋Šฅํ•˜๋‹ค)
  4. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์ต๋ช…ํ•จ์ˆ˜๊ฐ€ ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜๋Š”๋ฐ ๋„ค์ž„๋“œ ํŽ‘์…˜์€ ๋””๋ฒ„๊น… ๋˜๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ž์‹  ์Šค์Šค๋กœ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ (์ด๊ฑด์•„์ง ใ… )

ํ•จ์ˆ˜ ์„ ์–ธ์‹์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ๋งŒ ์ต์ˆ™ํ•ด์ ธ ์žˆ๋Š”๋ฐ ํ‘œํ˜„์‹์„ ์กฐ๊ธˆ ๋” ์ ๊ทน์ ์œผ๋กœ ์ฝ˜์†”์— ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค.

  1. Arrow Function

๊ธˆ์š”์ผ๋‚  ํŽ˜์–ด๋‹˜์—๊ฒŒ ๋“ฃ๊ธฐ๋Š” ํ–ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋ญ”๊ฐ€ ์‹ถ์—ˆ๋Š”๋ฐ ์˜ค๋Š˜ ์•Œ๊ฒŒ ๋˜๊ณ ๋‚˜๋‹ˆ ์ •๋ง ํŽธ๋ฆฌํ•œ ๊ฑฐ์˜€๊ตฌ๋‚˜ ์‹ถ๋‹ค.

const simplePrint = function() {
  console.log('simplePrint')
}

์ด ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ํ•œ ์ค„๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. Arrow Function ์œผ๋กœ.. ์‰ฝ๊ฒŒ! ๋งํ•˜๋ฉด function ๊ธ€์žํ•˜๊ณ  curly bracket ๋ถ€๋ถ„๋งŒ์„ ๋‚ ๋ ค ๋ฒ„๋ฆฌ๋Š” ๊ฑฐ๋‹ค. ๋Œ€์‹  => ์š”๋Ÿฐ ๊ธฐํ˜ธ๋ฅผ ์จ์ฃผ๋Š”๋ฐ,

const simplePrint = () => console.log('simplePrint')

=> ์ด ํ™”์‚ดํ‘œ ๋‹ค์Œ์— ์˜ค๋Š” ๊ฑฐ๋Š” ๋ฆฌํ„ด ๊ฐ’์ด ๊ฑฐ์˜ ์œ ๋ ฅํ•˜๋‹ค.

ํ•œ๋ฒˆ ๋” ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด,

const add = function(a, b) {
  return a + b
}

์ด ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๋ฉด

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

์ด๋ ‡๋“ฏ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๊ณ  ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž‘์„ฑ ์‹œ ๋” ๋น›์„ ๋ฐœํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ฌผ๋ก  ์ด๊ฑฐ ์•Œ๊ฒŒ ๋œ ์‹œ์ ์—๋Š” ์ง„์งœ ๋ณธ๋ก ์ธ ๋ฐฐ์—ด ํ•จ์ˆ˜๋ฅผ ์•„๋Š”๋ฐ ๋ญ ์ค‘์š”ํ•˜๋ƒ ๋Œ€์ˆ˜๋กญ์ง€ ์•Š๊ฒŒ ์—ฌ๊ฒผ๋‹ค.

๋ฌดํŠผ ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•  ๋•Œ bracket ์„ ๋„ฃ์–ด arrow ์™€ ๊ฐ™์ด ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋•Œ๋Š” bracket ๊ณผ return ์„ ํ‘œ๊ธฐํ•ด ์ค˜์•ผ ํ•œ๋‹ค.

const simpleMultiply = (a, b) => {
  // do something more
  return a * b
}

๋‚ด ์ƒ๊ฐ์—” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€๋ฉด ํšจ๊ณผ๊ฐ€ ๋ฐฑ๋งŒ์ ์ด๋‹ค.

  1. ๋ฐ˜๋ณต๋ฌธ์˜ ์„ธ ๊ฐ€์ง€ ํ˜•ํƒœ

์ผ๋ฐ˜์ ์ธ ๋ฐ˜๋ณต๋ฌธ์˜ ํ˜•ํƒœ๋Š” ๊ณต๋ถ€ํ•˜๋Š” ์ž…์žฅ์—์„œ ๋„ˆ๋ฌด๋‚˜ ์ž˜ ์•Œ๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿผ ๋‚˜๋จธ์ง€ ๋‘๊ฐœ๋Š” ๋ญ˜๊นŒ?

let fruits = ['๐ŸŽ', '๐Ÿ‡', '๐ŸŒ', '๐Ÿ‰', '๐Ÿ‹']

// for of ๊ตฌ๋ฌธ
for (let fruit of fruits) {
  console.log(fruit)
}

// ๋Œ€๋ง์˜ forEach, ๋ฐฐ์—ด ๋‚ด ๊ฐ’๋งˆ๋‹ค ์ •ํ•ด์ง„ action (์ฝœ๋ฐฑํ•จ์ˆ˜)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
fruits.forEach(function(fruit, index, array) {
  console.log(fruit, index, array) // array ๋Š” ๋ณดํ†ต ์•ˆ๋ฐ›์•„์˜จ๋‹ค.
})

// ์ต๋ช…ํ•จ์ˆ˜๋Š” arrow function ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ฐฐ์› ๋‹ค!
fruits.forEach(fruit => console.log(fruit)) // ๋ฐฐ์—ด ๋‚ด value ๋“ค ๋งˆ๋‹ค ๋‚ด๊ฐ€ ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ตฌ๋‚˜!

์•ž์„  ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์— ๋Œ€ํ•ด์„œ๋„ ์ข€ ๋” ์ •ํ™•ํžˆ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์ด๊ฑฐ๋Š” Array Function ์„ ์•Œ๊ธฐ ์œ„ํ•ด์„œ ํ•„์ˆ˜๋Š” ์•„๋‹ˆ๋‹ค.

๊ทผ๋ฐ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋งํ•˜์ง€๋งŒ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ๋Œ€๋ฐ•์ด๋‹ค. ์ด๋Ÿฐ๊ฒŒ ์žˆ์—ˆ๋‚˜..

3. 10 Array Functions for HOF๐Ÿบ

๋ถ€์ œ๋Š” ๋ชจ๋ฅด๋ฉด ํ›„ํšŒํ•˜๋Š” ๋ฐฐ์—ด ํ•จ์ˆ˜ 10๊ฐ€์ง€ ์ธ๋ฐ, ์˜์ƒ์„ ๋ฉˆ์ถฐ ๋†“๊ณ  ์Šค์Šค๋กœ ์ฐพ์•„์„œ ๋จผ์ € ํ’€๊ณ  ์˜์ƒ์„ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ ๊ธˆ์š”์ผ๋‚  ๋ฉ˜ํƒˆ์ด ํ„ธ๋ ธ๋˜ ์ด์œ ๋ฅผ ์ ˆ์‹คํžˆ ๊นจ๋‹ซ๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ์•„์ดํŒจ๋“œ ๋…ธํƒ€๋นŒ๋ฆฌํ‹ฐ๋กœ ์˜ฎ๊ธด๋‹ค์Œ, ๋นˆ ์ฝ˜์†” ์ฐฝ์„ ํ•˜๋‚˜ ์—ด๊ณ  ํ•˜๋‚˜์”ฉ ํ’€์—ˆ๋‹ค. ํ•„๊ธฐ๋„ ํ•˜๋ฉด ์†์—์„œ ์ ๋Š” ๋‚ด์šฉ์ด ๋‡Œ๋กœ ์ „๋‹ฌ๋˜๋Š”๊ฑฐ ๊ฐ™์•„์„œ (๋‡Œ์ด์ง•).. ํ•„๊ธฐ๋„..

writeonipad

โš ๏ธ์ฃผ์˜! ์•„๋ž˜ ๋‚ด์šฉ์„ ์ง์ ‘ ํ•ด๋ณด๊ณ  ์‹ถ์€ ๋ถ„๋“ค์€ ๋„ˆ๋ฌด ์œ ์‹ฌํžˆ ๋ณด๋ฉด ์•ˆ๋œ๋‹ค!

https://youtu.be/3CUjtKJ7PJg ๋งํฌ๋Š” ์œ„์—~

3-1. make a string out of an array

์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ string ์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ฐฐ์—ด.join() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

{
  const fruits = ['apple', 'banana', 'orange']
  fruits.join('. ')
  ;('apple, banana, orange')
}

join api ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด ํ˜•ํƒœ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ” ์ฃผ๋Š”๋ฐ ์ฃผ์˜ํ•  ์ ์€ fruits ์˜ ๋ฐฐ์—ด ํ˜•ํƒœ๋Š” ๊ทธ๋Œ€๋กœ ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

3-2. make an array out of a string

์ด๋ฒˆ์—” ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด ๋ณธ๋‹ค.

{
  const fruits = '๐ŸŽ, ๐Ÿฅ, ๐ŸŒ, ๐Ÿ’'
  fruits.split(',') // fruits.split() ํ•˜๋ฉด ๋”ฐ์˜ดํ‘œ๊ฐ€ ๋ถ™์ง€๋งŒ ์š”์†Œ๊ฐ€ ์ „์ฒด ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰๋œ๋‹ค.
  ;['๐ŸŽ', ' ๐Ÿฅ', ' ๐ŸŒ', ' ๐Ÿ’']
  fruits.split(',', 2)
  ;['๐ŸŽ', ' ๐Ÿฅ'] // limit ์ธ์ž๋ฅผ 2๋กœ ๋„ฃ์–ด์ฃผ๋‹ˆ ์•ž์— 2๊ฐœ๋งŒ ๋ฐ˜ํ™˜๋œ๋‹ค. (optional)
}

3-3. make this array look like this: [5, 4, 3, 2, 1]

๋ฐฐ์—ด์„ ๋กœ๊พธ๊ฑฐ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์ด๋‹ค. (ใ…‡ใ…ˆใ…‡ใ…ˆ)

{
  const array = [1, 2, 3, 4, 5]
  array.reverse()
  console.log(array)
  ;[5, 4, 3, 2, 1]
}

์–˜๋Š” ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฒŒ ์›๋ณธ array ๊ฐ€ ๋’ค์ง‘ํžŒ ์ฑ„๋กœ ๋ฐ”๋€Œ์–ด ๋ฒ„๋ฆฐ๋‹ค. ๋ฆฌํ„ด๋„ ๋ฐ”๋€ ๊ฑฐ ์ž์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

3-4. make new array without the first two elements

๋ฐฐ์—ด์˜ ์š”์†Œ 1, 2 ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ์ด๋‹ค. ๋‘๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

{
  const array = [1, 2, 3, 4, 5]
  let newArr = array.splice(2)
  console.log(newArr)
  ;[3, 4, 5]
  // array ๋Š” [1, 2] ๋กœ ์›๋ณธ์ด ๋ฐ”๋€Œ์–ด ๋ฒ„๋ฆฐ๋‹ค.
}
{
  const array = [1, 2, 3, 4, 5]
  let newArr2 = array.slice(2)
  console.log(newArr)
  ;[3, 4, 5]
  // array ๋Š” [1, 2, 3, 4, 5] ๋กœ ์›๋ณธ์ด ๊ทธ๋Œ€๋กœ ์žˆ๋‹ค.
}

5 ~ 10 ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ class ์™€ object ์„ ์–ธ

class Student {
  constructor(name, age, enrolled, score) {
    this.name = name
    this.age = age
    this.enrolled = enrolled
    this.score = score
  }
}
const students = [
  new Student('A', 29, true, 45),
  new Student('B', 28, false, 80),
  new Student('C', 30, true, 90),
  new Student('D', 40, false, 66),
  new Student('E', 18, true, 88),
]

3-5. find a student with the score 90

์—ฌ๊ธฐ์„œ ๋น„๋กœ์†Œ ๊ทธ ์ด์•ผ๊ธฐ๊ฐ€ ์‹œ์ž‘๋œ๋‹ค.

ํ•™์ƒ์˜ ์ ์ˆ˜๊ฐ€ 90์ ์ธ ํ•™์ƒ ์ฐพ๊ธฐ ์ด๋‹ค. ์ฒ˜์Œ์— ์‘? ์ €๊ธฐ์žˆ๋Š”๋ฐ? ๋ฉ์ฒญํ•œ ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‚˜๋Š” ๋ฌธ์ œ์˜ ์›๋ž˜ ์˜๋„์™€ ๋‹ค๋ฅด๊ฒŒ filter ๋ฅผ ๊ฒ€์ƒ‰ํ•ด ๋ณด๊ณ  ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์˜ค์ง€๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

๋ฐค์ด ์–ด๋‘์›Œ ์ง‘์— ๊ฐ€๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ๋˜๋Š” ๊ฑฐ ๊ฐ™์• ์„œ ์ข‹์•˜๋‹ค.

const result = students.filter(student => student.score === 90);
console.log(result);
[Student]
0: Student {name: "C", age: 30, enrolled: true, score: 90}
length: 1
__proto__: Array(0)

์ด ๋ฐฉ๋ฒ•๋„ ์ข‹์•˜์ง€๋งŒ ์˜์ƒ์—์„œ ๋“œ๋ฆผ์ฝ”๋”ฉ ์—˜๋ฆฌ ์„ ์ƒ๋‹˜๊ป˜์„œ find ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ๋ ค ์ฃผ์…จ๋‹ค.

const result = students.find(function(student) {
  return student.score === 90 // .find ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” Boolean ์„ ๋ฆฌํ„ดํ•œ๋‹ค.
}) // ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•™์ƒ์˜ ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋Œ๋ฉด์„œ ์กฐ๊ฑด์ด true ๋•Œ๊นŒ์ง€ ์ฐพ๋Š”๋‹ค.
// ์ฐพ์œผ๋ฉด (true ๊ฐ€ ๋ฆฌํ„ด๋  ๋•Œ), find ํ•จ์ˆ˜๊ฐ€ ๋ฉˆ์ถ”๊ณ  ํ•ด๋‹น ์กฐ๊ฑด์˜ student ๊ฐ€ ๋ฆฌํ„ด๋œ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ํ•œ ์ค„๋กœ ํ‘œํ˜„์ด ๋œ๋‹ค!

const result = students.find(student => student.score === 90)

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋„ˆ๋ฌด ์ข‹๋‹ค!๐Ÿ˜

3-6. make an array of enrolled students

ํ•™์ƒ๋“ค ์ค‘ ์ˆ˜์—…์— ๋“ฑ๋กํ•œ (enrolled) ํ•™์ƒ๋“ค๋งŒ ๊ณจ๋ผ๋‚ด ๋ณธ๋‹ค. ์—ฌ๊ธฐ์„œ filter ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ–ˆ๋‹ค.

const result = students.filter(student => student.enrolled === true);
console.log(result);

(3) [Student, Student, Student]
0: Student {name: "A", age: 29, enrolled: true, score: 45}
1: Student {name: "C", age: 30, enrolled: true, score: 90}
2: Student {name: "E", age: 18, enrolled: true, score: 88}
length: 3
__proto__: Array(0)

๋ฐฐ์—ด ์„ธ ๊ฐœ๊ฐ€ ํŠ€์–ด๋‚˜์˜ค๊ณ , student.enrolled ์˜ ๊ฐ’์ด true ์ธ ๊ฒƒ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. const result = students.filter(student => student.enrolled); true ๋ฅผ ๊ตณ์ด ์ ์ง€ ์•Š์•„๋„ ๋˜์—ˆ๋‹ค.

3-7. make an array containing only the studentsโ€™ scores

// result should be: [45, 80, 90, 66, 88]

ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋งŒ ์™์™ ๋ฝ‘์•„์˜จ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋‚œ ๊ธˆ์š”์ผ ์Šฌ๋ž™์—์„œ ๊ณต์œ ๋œ ์‚ฌ์ง„ ํ•œ์žฅ์ด ๋– ์˜ฌ๋ž๋‹ค.

map filter reduce

์™์™ ๋ฝ‘์•„์˜จ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.. ๋งŒ๋“ ๋‹ค.. ๊ฐ€๊ณตํ•œ๋‹ค.. ๊ทธ๋ ‡๋‹ค! map!

const result = students.map(student => student.score);
console.log(result);
(5) [45, 80, 90, 66, 88]
0: 45
1: 80
2: 90
3: 66
4: 88
length: 5
__proto__: Array(0)

map ์€ ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์š”์†Œ ํ•œ๊ฐ€์ง€ ํ•œ๊ฐ€์ง€๋ฅผ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋ณ€ํ™˜ (mapping) ์‹œ์ผœ ์ค€๋‹ค. ์ฆ‰ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ€๊ณต๋˜๋Š” ๊ฒƒ์ด๋‹ค.

3-8. check if there is a student with the score lower than 50

ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๊ฐ€ 50์  ๋ณด๋‹ค ์•„๋ž˜์ธ ํ•™์ƒ์ด ์žˆ๋Š”์ง€, ์žˆ์œผ๋ฉด true ๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์ด๋‹ค.

๋‚˜๋Š” ์•„๊นŒ ํ–ˆ๋˜ .find ๋ฅผ ์จ์„œ,

let result = students.find(student => student.score < 50)
console.log(result);
Student {name: "A", age: 29, enrolled: true, score: 45}

๋“œ๋ฆผ์ฝ”๋”ฉ์—˜๋ฆฌ ์„ ์ƒ๋‹˜์€ some ์ด๋ž€ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์•Œ๋ ค์ฃผ์…จ๋‹ค.

let result = students.some(student => student.score < 50)
console.log(result)
true

์œ„์ฒ˜๋Ÿผ .some ์„ ์‚ฌ์šฉํ•˜๋ฉด student ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํ•˜๋‚˜๋ผ๋„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ์œผ๋ฉด true ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

๋ฐ˜๋Œ€๋Š” .every ๊ฐ€ ์žˆ๋‹ค.

let result = students.every(student => student.score < 50)
console.log(result)
false

์œ„์˜ every ๋Š” ๋ชจ๋“  student ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์— ๋งŒ์กฑํ•ด์•ผ true ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

3-9. compute studentsโ€™ average score

ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€ ํ‰๊ท  ์ ์ˆ˜ ๊ตฌํ•ด์˜ค๊ธฐ ์ด๋‹ค. ์ด๊ฑด ์ฒซ๋ฒˆ์งธ๋กœ ์ ์ˆ˜๋งŒ์„ ์™์™ ๋ฝ‘์•„ โ€˜์žฌ๊ฐ€๊ณตโ€™ ๋œ ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ๋จผ์ €์ด๋‹ค.

let scoreOfStudents = students.map(student => student.score)
// ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋งŒ ๋ฝ‘์•„์˜จ ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์‹ค๋ ค scoreOfStudents ๋ณ€์ˆ˜์— ๋‹ด๊ฒผ๋‹ค.

์ด์ œ reduce() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ฐจ๋ก€๋‹ค. ์˜์ƒ์„ ๋ฉˆ์ถฐ ๋†“๊ณ  ์ฐพ์•„๋ดค๋Š”๋ฐ mdn ์˜ ์‚ฌ์šฉ๋ฒ•์ด ์•ฝ๊ฐ„ ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ ธ์„œ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•  ๋งŒํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐพ์•„๋ณด๊ณ  ๊ทธ๊ฑฐ๋Œ€๋กœ ์ฝ˜์†”์— ์˜ฎ๊ฒจ๋ดค๋‹ค.

let scoreOfStudents = students.map(student => student.score)
let result = scoreOfStudents.reduce((acc, curr) => acc + curr) / students.length
console.log(result)
// acc ๋Š” ์ด์ „์˜ ๊ฐ’(์ถ•์ ๋˜๋Š” ๊ฐ’), curr ๋Š” ๋ฐ˜๋ณต์„ ๋„๋Š” ํ˜„์žฌ student ์˜ ์š”์†Œ, ์•„๋ž˜์ฒ˜๋Ÿผ ์‹œ์ž‘์ง€์ ์„ 0์œผ๋กœ ํ‘œ์‹œํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
// scoreOfStudents.reduce((acc, curr) => (acc + curr), 0) / students.length
73.8

๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๋ชจ์•„ ๋ˆ„์ ํ•ด ๋†“์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

3-10. make a string containing all the scores

// result should be: โ€˜45, 80, 90, 66, 88โ€™ ํ•™์ƒ๋“ค์˜ ๋ชจ๋“  ์ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ด๋‹ค.

let scoreOfStudents = students.map(student => student.score)
// map ๊ณผ join ์‚ฌ์ด์— filter ๋ฅผ ์จ์„œ ์ ์ˆ˜๊ฐ€ 50์  ์ด์ƒ์ธ ์• ๋“ค๋งŒ ์ถ”๋ ค๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.
let result = scoreOfStudents.join(', ')

3-11. Bonus Q10 sorted in ascending order

// result should be: โ€˜45, 66, 80, 88, 90โ€™ ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋ฅผ ๋‚ฎ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ๋’ค, ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ด๋‹ค.

let scoreOfStudents = students.map(student => student.score)
let sortBy = scoreOfStudents.sort((a, b) => a - b)
console.log(sortBy)
;(5)[(45, 66, 80, 88, 90)]

sortBy.join(', ')
;('45, 66, 80, 88, 90')

4. ๋Š๋‚€ ์ 

arr func1 arr func2

์†์œผ๋กœ ์•„์ดํŒจ๋“œ์— ํ•„๊ธฐ๋„ ํ•ด๋ณด๊ณ , ์ฝ˜์†”์ฐฝ์— ์ฐ์–ด ๋ณด๊ธฐ๋„ ํ•˜๋ฉด์„œ ์œ ํŠœ๋ธŒ์—์„œ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ฒŒ ๋˜๊ธฐ ์ „๊ณผ ๋งŽ์ด ๋‹ฌ๋ผ์ง„ ๊ฑฐ ๊ฐ™๋‹ค. ์ผ๋‹จ ์ด๋Ÿฌํ•œ ์˜์ƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค€ ๋‚˜์˜ ํŽ˜์–ด๋‹˜ (์žฌํ™๋‹˜) ์—๊ฒŒ ์ง„์‹ฌ ๊ฐ์‚ฌํ•˜๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋“œ๋ฆผ์ฝ”๋”ฉ ์—˜๋ฆฌ ๋‹˜์˜ ํŒฌ์ด ๋˜์–ด ๋ฒ„๋ ธ๋‹ค.

์•„์ง ์ฝ”ํ”Œ์ž‡ ๋ฌธ์ œ๋Š” ํ’€์ง€ ๋ชปํ–ˆ์ง€๋งŒ ์ถ”์„ ๋•Œ ๋ญ”๊ฐ€ ํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ž์‹ ๊ฐ์ด ์ƒ๊ฒผ๋‹ค.


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook