Front/JS & jQuery

[js] 배열 평탄화 flat()

오선지♬ 2024. 12. 6. 20:07
728x90
반응형
let nestedArray = [1, [2, 3], [4, [5, 6]]];

// 기본적으로 한 단계 평탄화
let flatArray = nestedArray.flat();

console.log(flatArray);
// [1, 2, 3, 4, [5, 6]]

// 모든 단계 평탄화 (Infinity 사용)
let fullyFlatArray = nestedArray.flat(Infinity);

console.log(fullyFlatArray);
// [1, 2, 3, 4, 5, 6]
728x90
반응형