Front-end/JS

μžλ°”μŠ€ν¬λ¦½νŠΈ λ°°μ—΄ κ³ μ°¨ν•¨μˆ˜ forEach / map

ciocio 2021. 8. 23. 14:13

πŸ“  Array.prototype.forEach ( immutable  β–³ )

 

 

ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° 은 순수 ν•¨μˆ˜μ™€ 보쑰 ν•¨μˆ˜μ˜ 쑰합을 톡해 둜직 내에 μ‘΄μž¬ν•˜λŠ” 쑰건문과 λ°˜λ³΅λ¬Έμ„ μ œκ±°ν•˜μ—¬

λ³΅μž‘μ„±μ„ ν•΄κ²°ν•˜κ³ , λ³€μˆ˜μ˜ μ‚¬μš©μ„ μ–΅μ œν•˜μ—¬ μƒνƒœ 변경을 ν”Όν•˜λ €λŠ” ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ΄λ‹€.

 

forEach λ©”μ„œλ“œλŠ” for문을 λŒ€μ²΄ν•  수 μžˆλŠ” κ³ μ°¨ν•¨μˆ˜λ‹€.

λ°˜λ³΅λ¬Έμ„ μΆ”μƒν™”ν•œ κ³ μ°¨ ν•¨μˆ˜λ‘œμ„œ λ‚΄λΆ€μ—μ„œ λ°˜λ³΅λ¬Έμ„ 톡해 μžμ‹ μ„ ν˜ΈμΆœν•œ 배열을 μˆœνšŒν•˜λ©΄μ„œ

μˆ˜ν–‰ν•΄μ•Ό ν•  처리λ₯Ό 콜백 ν•¨μˆ˜λ‘œ 전달받아 반볡 ν˜ΈμΆœν•œλ‹€.

 

const number = [1, 2, 3];
const pows = [];

numbers.forEach(item => pows.push(item ** 2));
console.log(pows);  // [1, 4, 9];

 

πŸ””  forEach λ©”μ„œλ“œλŠ”  μ΅œλŒ€  2개의 인수λ₯Ό 받을 수 μžˆλ‹€.

 

01  콜백 ν•¨μˆ˜

02  'this'둜 μ‚¬μš©ν•  객체 ( 일단 class ν•œμ • )

 

 

πŸ””  forEach λ©”μ„œλ“œμ˜ μ½œλ°±ν•¨μˆ˜λŠ” μ΅œλŒ€ 3개의 인수λ₯Ό 받을 수 μžˆλ‹€.

 

01  forEach λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄μ˜ μš”μ†Œκ°’

02  forEach λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄μ˜ 인덱슀

03  forEach λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄ 자체 ( this )

 

 

 

🏁  속성

 

[1, 2, 3].forEach((item, index, arr) => {
  console.log(`μš”μ†Œκ°’: ${item}, 인덱슀: ${index}, this: ${JSON.stringify(arr)}`);
  return item;  // λ§Ήλͺ©μ  return
});

/*
μš”μ†Œκ°’: 1, 인덱슀: 0, this: [1,2,3]
μš”μ†Œκ°’: 2, 인덱슀: 1, this: [1,2,3]
μš”μ†Œκ°’: 3, 인덱슀: 2, this: [1,2,3]
*/

 

β—Ύ JSON.stringify λ©”μ„œλ“œ ?

 

이 λ©”μ„œλ“œλŠ” 객체λ₯Ό JSON 포맷의 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•œλ‹€. μœ„ μ˜ˆμ œμ—μ„œλŠ” 객체인 arr 배열을 ( λ°°μ—΄ ⊂ 객체 ) λ¬Έμžμ—΄λ‘œ 좜λ ₯ν•˜κΈ° μœ„ν•΄ 씀

 

 

const numbers = [1, 2, 3];

// forEach λ©”μ„œλ“œλŠ” 원본 배열을 λ³€κ²½ν•˜μ§€λŠ” μ•Šμ§€λ§Œ !!
// 콜백 ν•¨μˆ˜λ₯Ό 톡해 원본 배열을 λ³€κ²½ν•  μˆ˜λŠ” μžˆλ‹€. ν•  수 λŠ” 있 λ‹€ ν•˜ ν•˜

numbers.forEach((item, index, arr) => { arr[index] = item ** 2 });
console.log(numbers);  // [1, 4, 9]

 

 

❗ forEach λ©”μ„œλ“œμ˜ λ°˜ν™˜κ°’μ€ μ–Έμ œλ‚˜ undefined이닀 ❗

 

let arr = [];

const result = [1, 2, 3].forEach(item => arr.push(item + 2));
console.log(result);  // undefined

 

class Numbers {
  numberArray = [];
  
  multiply(arr) {
    arr.forEach(function(item){
      this.numberArray.push(item * item);
    }, this);  
    // μ΄λ ‡κ²Œ 2번째 인자둜 this 전달 μ•ˆν•΄μ£Όλ©΄ this.numberArray의 thisλŠ” undefined λœΉλ‹ˆλ‹€
    // 이유1: 클래슀 λ‚΄λΆ€μ˜ λͺ¨λ“  μ½”λ“œμ—λŠ” μ•”λ¬΅μ μœΌλ‘œ strict modeκ°€ μ μš©λœλ‹€.
    // 이유2: forEach에 ν™”μ‚΄ν‘œ ν•¨μˆ˜κ°€ μ•„λ‹Œ 일반 ν•¨μˆ˜κ°€ λ“€μ–΄μ™€μžˆλ‹€.
  }
}

const numbers = new numbers();
numbers.multiply([1, 2, 3]);
console.log(numbers.numberArray);  // [1, 4, 9]

 

class Numbers {
  numberArray = [];
  
  multiply(arr) {
    arr.forEach(item => this.numberArray.push(item * item));
  }
}

const numbers = new numbers();
numbers.multiply([1, 2, 3]);
console.log(numbers.numberArray);  // [1, 4, 9]

 

 

 

 

πŸ“  Array.prototype.map ( immutable )

 

 

map λ©”μ„œλ“œλŠ” μžμ‹ μ„ ν˜ΈμΆœν•œ λ°°μ—΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό μˆœνšŒν•˜λ©΄μ„œ 인수둜 전달받은 콜백 ν•¨μˆ˜λ₯Ό 반볡 ν˜ΈμΆœν•œλ‹€.

콜백 ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’λ“€λ‘œ κ΅¬μ„±λœ μƒˆλ‘œμš΄ 배열을 λ°˜ν™˜ν•œλ‹€. μ΄λ•Œ 원본 배열은 λ³€κ²½λ˜μ§€ μ•ŠλŠ”λ‹€.

 

const numbers = [1, 4, 9];

const roots = numbers.map(item => Math.sqrt(item));

// μœ„ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.
// const roots = numbers.map(Math.sqrt);

console.log(roots);  // [1, 2, 3]
console.log(numbers);  // [1, 4, 9]

 

πŸ””  map λ©”μ„œλ“œλŠ”  μ΅œλŒ€  2개의 인수λ₯Ό 받을 수 μžˆλ‹€.

 

01  콜백 ν•¨μˆ˜

02  'this'둜 μ‚¬μš©ν•  객체 ( 일단 class ν•œμ • )

 

 

πŸ””  map λ©”μ„œλ“œμ˜ μ½œλ°±ν•¨μˆ˜λŠ” μ΅œλŒ€ 3개의 인수λ₯Ό 받을 수 μžˆλ‹€.

 

01  map λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄μ˜ μš”μ†Œκ°’

02  map λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄μ˜ 인덱슀

03  map λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œ λ°°μ—΄ 자체 ( this )

 

 

 

🏁  속성

 

[1, 2, 3].map((item, index, arr) => {
  console.log(`μš”μ†Œκ°’: ${item}, 인덱슀: ${index}, this: ${JSON.stringify(arr)}`);
  return item;  // λ§Ήλͺ©μ  return
});

/*
μš”μ†Œκ°’: 1, 인덱슀: 0, this: [1,2,3]
μš”μ†Œκ°’: 2, 인덱슀: 1, this: [1,2,3]
μš”μ†Œκ°’: 3, 인덱슀: 2, this: [1,2,3]
*/

 

// map λ©”μ„œλ“œμ˜ 두 번째 인수둜 map λ©”μ„œλ“œμ˜ 콜백 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ this둜 μ‚¬μš©ν•  객체λ₯Ό 전달할 수 μžˆλ‹€.

class Prefixer {
  constructor(prefix){
    this.prefix = prefix;
  }
  
  add(arr){
    return arr.map(function(item){
      return this.prefix + item;
      // μ™ΈλΆ€μ—μ„œ thisλ₯Ό μ „λ‹¬ν•˜μ§€ μ•ŠμœΌλ©΄ thisλŠ” undefinedλ₯Ό 가리킨닀.
    }, this);  // map λ©”μ„œλ“œμ˜ 콜백 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ this둜 μ‚¬μš©ν•  객체λ₯Ό μ „λ‹¬ν•œλ‹€.
  }
}

const prefixer = new Prefixer('-webkit-');
console.log(prefixer.add(['transition', 'user-select']));

// ['-webkit-transition', '-webkit-user-select']

 

// 두 번째 인수λ₯Ό 톡해 this μ „λ‹¬ν•˜λŠ” 방법말고 !! 더 λ‚˜μ€ 방법 -> ν™”μ‚΄ν‘œ ν•¨μˆ˜ μ΄μš©ν•˜κΈ°

class Prefixer {
  constructor(prefix){
    this.prefix = prefix;
  }
  
  add(arr){
    return arr.map(item => this.prefix + item);
    // ν™”μ‚΄ν‘œ ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ thisλ₯Ό μ°Έμ‘°ν•˜λ©΄ μƒμœ„ μŠ€μ½”ν”„μ˜ thisλ₯Ό κ·ΈλŒ€λ‘œ μ°Έμ‘°ν•œλ‹€ γ„·γ„·πŸ‘
  }
}

const prefixer = new Prefixer('-webkit-');
console.log(prefixer.add(['transition', 'user-select']));

// ['-webkit-transition', '-webkit-user-select']

 

 

 

β—Ύ forEach λ©”μ„œλ“œ vs map λ©”μ„œλ“œ

 

00  곡톡점

 

μžμ‹ μ„ ν˜ΈμΆœν•œ λ°°μ—΄μ˜ λͺ¨λ“  μš”μ†Œλ₯Ό μˆœνšŒν•˜λ©΄μ„œ 인수둜 전달받은 콜백 ν•¨μˆ˜λ₯Ό 반볡 ν˜ΈμΆœν•œλ‹€.

 

00  차이점

 

forEach λ©”μ„œλ“œλŠ” μ–Έμ œλ‚˜ undefinedλ₯Ό λ°˜ν™˜ν•œλ‹€.

map λ©”μ„œλ“œλŠ” 콜백 ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’λ“€λ‘œ κ΅¬μ„±λœ μƒˆλ‘œμš΄ 배열을 λ°˜ν™˜ν•œλ‹€.

 

즉, forEach λ©”μ„œλ“œλŠ” λ‹¨μˆœνžˆ λ°˜λ³΅λ¬Έμ„ λŒ€μ²΄ν•˜λŠ” κ³ μ°¨ ν•¨μˆ˜μ΄κ³ 

map λ©”μ„œλ“œλŠ” μš”μ†Œκ°’μ„ κ°€λ₯Έ κ°’μœΌλ‘œ λ§€ν•‘ν•œ μƒˆλ‘œμš΄ 배열을 μƒμ„±ν•˜λŠ” κ³ μ°¨ ν•¨μˆ˜μ΄λ‹€.

 

 

λ°˜μ‘ν˜•