young
is this it
young
전체 방문자
오늘
어제
  • 분류 전체보기 (143)
    • 웹_프론트엔드 (1)
      • 로드맵 챌린지 (73)
      • Svelte (2)
      • React (6)
      • JavaScript (8)
      • TypeScript (2)
      • HTML+CSS (5)
    • 웹_백엔드 (0)
      • Django (0)
    • 빅데이터 (33)
      • R (30)
      • Python (2)
    • 기타 (11)
      • git (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • form
  • 공개키
  • css네이밍
  • ssl
  • 버전관리
  • 대칭키
  • owasp
  • rstudio지도정보
  • 암호화
  • 구글맵api
  • 태스크러너
  • 웹보안
  • css후처리기
  • ggplot
  • bem
  • Regex
  • 인증
  • 보안취약점
  • ggmap()
  • vcs

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
young

is this it

웹_프론트엔드/JavaScript

[자바스크립트] 배열 안에 배열이 있을때 flat한 배열로 만들기

2023. 12. 5. 11:27
반응형

1. 최신 자바스크립트 API인 flat() 사용

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

 

Array.prototype.flat() - JavaScript | MDN

The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

developer.mozilla.org

 

2. 직접 메소드 작성해보기

// arr: 배열, d: 차원
const flatDeep = (arr: any[], d = 1): any[] => {
    return d > 0
        ? arr.reduce((acc, val) => {
              return acc.concat(
                  Array.isArray(val) ? flatDeep(val, d - 1) : val
              ) as any[];
          }, [])
        : arr.slice();
};

 

반응형

'웹_프론트엔드 > JavaScript' 카테고리의 다른 글

[자바스크립트] 객체 불변성(Immutability)이 중요한 이유  (0) 2024.05.11
[자바스크립트] 이미지 URL을 File 또는 FileList로 변환하기  (0) 2023.11.10
[JS] 클로저란?  (0) 2023.07.25
[JS] 스코프(Scope)란?  (0) 2023.07.24
[JS] 연속된 정수 배열을 만드는 다양한 방법  (0) 2023.07.14
    '웹_프론트엔드/JavaScript' 카테고리의 다른 글
    • [자바스크립트] 객체 불변성(Immutability)이 중요한 이유
    • [자바스크립트] 이미지 URL을 File 또는 FileList로 변환하기
    • [JS] 클로저란?
    • [JS] 스코프(Scope)란?
    young
    young

    티스토리툴바