Array.flat 배열 풀기

leehowook·2022년 7월 29일
0

Array.flat은 중첩 배열을 원하는 숫자만큼 풀어서 새로운 Array를 return 해준다.

뿐만 아니라 Array내에 빈 배열이 있을경우 삭제 시켜준다.

flat()내에 파라미터값을 넣지않으면 기본값은 1이며 Infinity를 이용해서

중첩의 양의 상관없이 다 풀수도 있다.

const numbers = [1, [2, [3, [4, [5, [6]]]]]];
 
const result = numbers.flat();
const result2 = numbers.flat(3);
const result3 = numbers.flat(Infinity);
 
// [1, 2, [3, [4, [5, 6]]]]]
console.log(result);
 
// [1, 2, 3, 4, [5, [6]]]
console.log(result2);
 
// [1, 2, 3, 4, 5, 6]
console.log(result3);

출처:https://nanggi-hl.tistory.com/199

profile
be higher

0개의 댓글