
const arr = [1, [2, [3, [4]]]];
arr.flat(1) → 배열을 한 단계만 펼침: [1, 2, [3, [4]]]
arr.flat(2) → 두 단계까지 펼침: [1, 2, 3, [4]]
arr.flat(3) → 세 단계까지 펼침: [1, 2, 3, 4]
const result = arr.flat(Infinity);
console.log(result); // [1,2,3,4]
const array1 = [1, 4, 9, 16];
const map1 = array1.map((x) => x * 2);
console.log(map1); // [2, 8, 18, 32]
map()과 flat(1)을 합친 기능
const arr = [1, 2, 3];
const result = arr.flatMap(x => [x, x * 2]);
console.log(result); // [1, 2, 2, 4, 3, 6]
map() 역할: 각 요소에 함수를 적용해 [1, 2], [2, 4], [3, 6]을 만듦.
flat(1) 역할: 결과 배열을 한 단계 펼쳐서 [1, 2, 2, 4, 3, 6]으로 만듦.