07-4. 배열내장 함수(map)
배열내장 함수
map
const array = [1, 2, 3, 4, 5, 6, 7, 8];// ❌ Bad Code
const array = [1, 2, 3, 4, 5, 6, 7, 8];
// *1번 방법*
const squared = [];
for (let i = 0; i < array.length; i++) {
squared.push(array[i] * array[i]);
}
// *2번 방법* 1번 방법보다 더 깔끔하게 작성이 가능하다.
const squared = [];
array.forEach(n => {
squared.push(n * n);
});
// *3번 방법* 2번 방법보다 더 깔끔하게 작성이 가능하다.
const square = n => n * n;
const squared = array.map(square);
console.log(squared);map을 활용해서 다양한것을 해보자
map 활용한 예시

Last updated