TIL05. JavaScript : Callback Function in Array

ID짱재·2021λ…„ 9μ›” 2일
0

JavaScript

λͺ©λ‘ 보기
17/19
post-thumbnail

πŸ“Œ 이 ν¬μŠ€νŒ…μ—μ„œλŠ” JavaScript의 λ°°μ—΄μ—μ„œ 자주 μ“°μ΄λŠ” 콜백 ν•¨μˆ˜μ— λŒ€ν•΄μ„œ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.



🌈 Callback Function in Array

πŸ”₯ What's Callback Function

πŸ”₯ Useful Callback Function in Array



1. What's Callback Function


πŸ€” 콜백 ν•¨μˆ˜λž€ 뭘까?

βœ”οΈ Javascript의 λ³€μˆ˜μ—λŠ” μˆ«μžλ‚˜ λ¬Έμžμ—΄ 같은 데이터, 객체뿐만 μ•„λ‹ˆλΌ ν•¨μˆ˜λ„ λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆμŠ΅λ‹ˆλ‹€.
βœ”οΈ λ³€μˆ˜μ— ν•¨μˆ˜λ₯Ό ν• λ‹Ήν•œλ‹€λŠ” μ˜λ―ΈλŠ” ν•¨μˆ˜λ₯Ό νŒŒλΌλ―Έν„°λ‘œ μ „λ‹¬ν•˜κ±°λ‚˜ ν•¨μˆ˜ μ•ˆμ—μ„œ 또 λ‹€λ₯Έ ν•¨μˆ˜κ°€ μž‘λ™λ˜κ³ , λ°˜ν™˜ν•  수 μžˆλ‹€λŠ” μ˜λ―Έμž…λ‹ˆλ‹€.
βœ”οΈ 즉, μ½œλ°±ν•¨μˆ˜(Callback Function)λž€ νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜λ₯Ό 전달받아, ν•¨μˆ˜μ˜ λ‚΄λΆ€μ—μ„œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
βœ”οΈ 이에 μ½œλ°±ν•¨μˆ˜λŠ” 슀슀둜 μ‹€ν–‰λ˜λŠ”κ²Œ μ•„λ‹ˆλΌ, μžμ‹ μ΄ μ „λ‹¬λ˜μ–΄μ§€λŠ” ν•¨μˆ˜κ°€ 싀행될 λ•ŒκΉŒμ§€ 기닀리닀가 ν˜ΈμΆœμ— 따라 μž‘λ™λ©λ‹ˆλ‹€.

function myCallBackFunc() { # πŸ‘ˆ callback ν•¨μˆ˜
    console.log('The callback function has been executed.');
}
function sayHello(name, callback) {
    console.log(`Hello ${name}! Call the callback function below.`);
    callback();
    console.log('End of "sayHello" function.');
}
sayHello('Jaewon', myCallBackFunc);
// Hello Jaewon! Call the callback function below.
// The callback function has been executed. # πŸ‘ˆ callback ν•¨μˆ˜μ˜ μ‹€ν–‰ κ²°κ³Ό
// End of "sayHello" function.

πŸ€” 콜백 ν•¨μˆ˜λŠ” μ™œ 읡λͺ…ν•¨μˆ˜λ‘œ μ‚¬μš©ν• κΉŒ?

βœ”οΈ μ½œλ°±ν•¨μˆ˜λ₯Ό 읡λͺ…ν•¨μˆ˜λ‘œ μ‚¬μš©ν•΄μ•Όλ§Œ ν•˜λŠ” 것은 μ•„λ‹™λ‹ˆλ‹€. ν•¨μˆ˜μ˜ 이름을 뢙여쀄 수 있죠.
βœ”οΈ ν•˜μ§€λ§Œ, 일반적으둜 μ½œλ°±ν•¨μˆ˜λŠ” μ–΄λ–€ ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜κΈΈ κΈ°λ‹€λ Έλ‹€κ°€ μž‘λ™ν•˜κΈ° λ•Œλ¬Έμ— ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μž‘λ™λ˜κ³ , 이에 ν•¨μˆ˜μ˜ 이름이 ν•„μš”ν•˜μ§€ μ•Šμ„ κ²½μš°κ°€ 많기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
βœ”οΈ 즉, ν•¨μˆ˜λ₯Ό μ—¬λŸ¬ κ³³μ—μ„œ μž¬ν™œμš©ν•  ν•„μš”κ°€ 없이 ν•΄λ‹Ή ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλ§Œ μž‘λ™λœλ‹€λ©΄, ꡳ이 ν•¨μˆ˜λͺ…을 μ‚¬μš©ν•  ν•„μš”κ°€ μ—†κΈ° λ•Œλ¬Έμ— 읡λͺ…ν•¨μˆ˜λ‘œ 자주 μ‚¬μš©λ©λ‹ˆλ‹€.

function sum(a,b){
  return a+b
}
function myFunc(a,b,callback){
  let newA = a*10 // 20
  let newB = b/2  // 5
  let res = callback(newA, newB)
  console.log(res)
}
myFunc(2,10,sum) // 25

βœ”οΈ λ˜ν•œ 콜백 ν•¨μˆ˜μ˜ 가독성과 간결함을 μœ„ν•΄ ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ‚˜νƒ€λ‚΄κΈ°λ„ ν•©λ‹ˆλ‹€. μœ„ ν•¨μˆ˜λ₯Ό ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ§Œλ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

function myFunc(a,b){
  let newA = a*10 // 20
  let newB = b/2  // 5
  let callback = (a, b) => {console.log(a+b)}
  callback(newA, newB)
}
myFunc(2,10) // 25


3. Useful Callback Function in Array


πŸ€” find()

βœ”οΈ find() : find λ§€μ„œλ“œλŠ” 주어진 λ₯Ό λ§Œμ‘±ν•˜λŠ” 첫 번째 μš”μ†Œμ˜ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€. 만일 콜백 ν•¨μˆ˜μ—μ„œ μš”κ΅¬ν•˜λŠ” 쑰건에 ν•΄λ‹Ήν•˜λŠ” μ›μ†Œκ°€ μ—†λ‹€λ©΄, "undefined"을 λ°˜ν™˜ν•©λ‹ˆλ‹€. filter λ§€μ„œλ“œμ™€ 차이점은 filterλŠ” 쑰건에 ν•΄λ‹Ήν•˜λŠ” μ›μ†Œλ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜ν•˜κ³ , find λ§€μ„œλ“œλŠ” 쑰건에 ν•΄λ‹Ήν•˜λŠ” 첫번째 μ›μ†Œ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

students = [10, 80, 99, 40, 85, 32, 40, 98, 22, 78, 82, 85, 91, 90, 85]
let result = students.find((value) => value === 85); 
console.log(result); // 85

βœ”οΈ μœ„ ν•¨μˆ˜λ₯Ό μ•„λž˜ ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ§Œλ“€λ©΄ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

students = [10, 80, 99, 40, 85, 32, 40, 98, 22, 78, 82, 85, 91, 90, 85]
function callback(value){
  if(value===85){
    return value
  }
}
let result = students.find(callback); 
console.log(result); // 85

πŸ€” filter()

βœ”οΈ filter(callbackFunction, thisAgr) : filter() λ©”μ„œλ“œλŠ” 배열을 μš”μ†Œλ₯Ό μˆœνšŒν•˜λ©° μ½œλ°±ν•¨μˆ˜μ— μ „λ‹¬ν•˜κ³ , μ½œλ°±ν•¨μˆ˜μ— ν•΄λ‹Ήν•˜λŠ” μš”μ†Œλ“€λ§Œ λͺ¨μ•„μ„œ μƒˆλ‘œμš΄ 배열을 λ°˜ν™˜ν•΄μ€λ‹ˆλ‹€. λ§Œμ•½ μ½œλ°±ν•¨μˆ˜μ˜ 쑰건에 ν•΄λ‹Ήν•˜λŠ” μš”μ†Œκ°€ 없을 λ•Œμ—λŠ” 빈 배열을 λ°˜ν™˜ν•©λ‹ˆλ‹€. 즉, filter λ§€μ„œλ“œλŠ” 원본 배열에 영ν–₯을 λ―ΈμΉ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이와 ν•¨κ»˜ μ½œλ°±ν•¨μˆ˜μ€ 3개의 인자(element, index, array)λ₯Ό κ°€μ§€λŠ”λ°, 첫번째 인자만 ν•„μˆ˜μ΄κ³  λ‹€λ₯Έ μΈμžλŠ” μ„ νƒμž…λ‹ˆλ‹€.

let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
function callback(value){
  if(value.includes('go')){
    return true
  }
}
let result = fruits.filter(callback);
console.log(fruits) // [ 'apple', 'banana', 'grapes', 'mango', 'orange' ]
console.log(result) // [ 'mango' ]

βœ”οΈ μœ„ ν•¨μˆ˜λ₯Ό μ•„λž˜ ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ§Œλ“€λ©΄ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
let result = fruits.filter((value) => value.includes('go'));
console.log(fruits) // [ 'apple', 'banana', 'grapes', 'mango', 'orange' ]
console.log(result) // [ 'mango' ]

πŸ€” map()

βœ”οΈ map(): λ°°μ—΄ λ‚΄ μ›μ†Œλ“€μ„ 순차적으둜 μˆœνšŒν•˜λ©°, μ½œλ°±ν•¨μˆ˜μ— 전달해 값을 μ²˜λ¦¬ν•œ λ’€ λ°°μ—΄λ‘œ λ°˜ν™˜ν•΄μ€λ‹ˆλ‹€.

students = [10, 20, 30, 40, 50]
function callback(value){
  return value * 2
}
let result = students.map(callback); 
console.log(result); // [ 20, 40, 60, 80, 100 ]

βœ”οΈ μœ„ ν•¨μˆ˜λ₯Ό μ•„λž˜ ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ λ§Œλ“€λ©΄ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

students = [10, 20, 30, 40, 50]
let result = students.map((value) => value * 2); 
console.log(result); // [ 20, 40, 60, 80, 100 ]

πŸ€” some & every

βœ”οΈ some은 주어진 배열을 μˆœνšŒν•˜λ©° callback ν•¨μˆ˜μ— μ›μ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬, callback ν•¨μˆ˜ 쑰건에 μΌμΉ˜ν•œ μ›μ†Œκ°€ ν•˜λ‚˜λΌλ„ 있으면, trueλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 즉, some λ§€μ„œλ“œλŠ” λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œκ°€ callback ν•¨μˆ˜μ˜ 쑰건에 μΌμΉ˜ν•˜μ§€ μ•Šμ„ λ•Œλ§Œ falseλ₯Ό λ°˜ν™˜μ‹œν‚΅λ‹ˆλ‹€.

let score1 = [98, 67, 45, 90, 77, 52];
let resultSome1 = score1.some((value) => value >= 95); 
console.log(resultSome1); // true
let score2 = [98, 60, 88, 90, 77, 69];
let resultSome2 = score2.some((value) => value <= 50); 
console.log(resultSome2); // false

βœ”οΈ 이와 λ°˜λŒ€λ‘œ everyλŠ” 주어진 배열을 μˆœνšŒν•˜λ©° callback ν•¨μˆ˜μ— μ›μ†Œλ₯Ό μ „λ‹¬ν•˜μ—¬, callback ν•¨μˆ˜ 쑰건에 μΌμΉ˜ν•˜μ§€ μ•ˆμ€ μ›μ†Œκ°€ ν•˜λ‚˜λΌλ„ 있으면, falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 즉, every λ§€μ„œλ“œλŠ” λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œκ°€ callback ν•¨μˆ˜μ˜ 쑰건에 λͺ¨λ‘ μΌμΉ˜ν•  λ•Œλ§Œ trueλ₯Ό λ°˜ν™˜μ‹œν‚΅λ‹ˆλ‹€.

let score1 = [98, 67, 45, 90, 77, 52];
let score2 = [98, 91, 88, 93, 94, 82];
let resultEvery1 = score1.every((value) => value >= 50); 
console.log(resultEvery1); // false
let resultEvery2 = score2.every((value) => value >= 80); 
console.log(resultEvery2); // true
profile
Keep Going, Keep Coding!

0개의 λŒ“κΈ€